
출처 : 위키백과

출처 : 위키백과
자료 이동 횟수가 결정된다.
2번째, 3번째 등 k번 째로 큰 값을 찾는 경우 유용하게 사용할 수 있다.
public class Selection_Sort {
public static void main(String[] args) {
// 선택정렬 알고리즘(Selection Sort Algorithm)
int[] arr = {9, 1, 6, 8, 4, 3, 2, 0};
for(int i = 0; i < arr.length; i++) {
// +1을 하는 이유는 자기와 비교할 필요가 없기 때문
for(int j = i + 1; j < arr.length; j++) {
// '>' 일 경우 오름차순 '<' 일 경우 내림차순
if(arr[i] > arr[j]) {
int temp = arr[i]; // 값 변경해야 하기에 임시 저장
arr[i] = arr[j]; // j를 i로 변경
arr[j] = temp; // i를 j로 변경
}
}
}
for(int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + " "); // 정렬 후 결과 출력
}
}
}

출처 : https://play.google.com/store/apps/details?id=wiki.algorithm.algorithms&hl=ko&gl=US
public class Bubble_Sort {
public static void main(String[] args) {
int arr[] = {1, 5, 2, 3, 4, 8};
// 배열의 길이를 -1 해준 이유는 사이클 마지막 떄 정렬이 이미 되어 있다
// -1을 안해주면 반복문이 한 사이클 더 돌게 된다.
for(int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length - 1 - i; j++) {
if (arr[j] > arr[j + 1]) {
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}

출처 : https://ko.wikipedia.org/wiki/%EC%82%BD%EC%9E%85_%EC%A0%95%EB%A0%AC
public class Insertion_Sort {
public static void main(String[] args) {
int arr[] = {1, 5, 2, 3, 4, 8};
for(int i = 1 ; i < arr.length ; i++){
int temp = arr[i];
int aux = i - 1;
while( (aux >= 0) && ( arr[aux] > temp ) ) {
arr[aux+1] = arr[aux];
aux--;
}
arr[aux + 1] = temp;
}
for(int i=0; i<arr.length; i++) {
System.out.print(arr[i] + " ");
}
}
}
[6, 5, 3, 1, 8, 7, 2, 4]
먼저 하나의 배열을 두 개로 쪼갭니다.
[6, 5, 3, 1][8, 7, 2, 4]
그래고 다시 두 개의 배열을 네 개로 쪼갭니다.
[6, 5][3, 1] [8, 7][2, 4]
마지막으로 디시 네 개의 배열을 여덜 개로 쪼갭니다.
[6][5] [3][1] [8][7] [2][4]
이제 더 이상 쪼갤 수가 없으니 두 개씩 합치기를 시작하겠습니다. 합칠 때는 작은 숫자가 앞에 큰 수자를 뒤에 위치시킵니다.
[5, 6][1, 3] [7, 8][2, 4]
이제 4개의 배열을 2개로 합칩니다. 각 배열 내에서 가장 작은 값 2개를 비교해서 더 작은 값을 먼저 선택하면 자연스럽게 크기 순으로 선택이 됩니다.
[1, 3, 5, 6][2, 4, 7, 8]
최종적으로 2개의 배열도 마찬가지로 크기 순으로 선택하가면서 하나로 합치면 정렬된 배열을 얻을 수 있습니다.
[1, 2, 3, 4, 5, 6, 7, 8]

출처 : https://ko.wikipedia.org/wiki/%ED%95%A9%EB%B3%91_%EC%A0%95%EB%A0%AC
public class Merge_Sort {
public static int[] buff; //임시 배열
public static void mergeSort(int[] arr, int left, int right) {
if(left < right) {
int i;
int center = (left + right) / 2;
int p = 0; //임시 배열 인덱스
int j = 0;
int k = left; //원본 배열 인덱스
mergeSort(arr, left, center); //배열 앞부분 병합정렬
mergeSort(arr, center+1, right); //배열 뒷부분 병합정렬
for(i = left; i <= center; i++) { //임시 배열에 배열 앞부분을 넣어줌.
buff[p++] = arr[i];
}
while(i <= right && j < p) { //원본 배열의 뒷부분, 추가 배열의 앞부분의 원소끼리 비교한다.
arr[k++] = buff[j] <= arr[i] ? buff[j++] : arr[i++];
}
while(j < p) { //임시 배열의 앞부분이 모두 커서 선택이 안됐다면 원본배열 뒷부분에 붙혀준다.
arr[k++] = buff[j++]; //여기서 왜 i < right는 비교를 안할까? -> 비교를 하면서 선택을 못받은 잠재적 i값(큰 값)들은 원본배열 뒷부분에 있기 때문이다.
}
}
}
public static void main(String[] args) {
int[] arr = {3, 4, 2, 1, 6, 8, 9, 5};
int N = arr.length;
buff = new int[N];
mergeSort(arr, 0, N - 1);
for (int a : arr) {
System.out.print(a + " ");
}
}
}
출처 : https://tosuccess.tistory.com/127

출처 : https://ko.wikipedia.org/wiki/%ED%80%B5_%EC%A0%95%EB%A0%AC
public class Quick_Sort {
static int partition(int[] array,int start, int end) {
int pivot = array[(start+end)/2];
while(start<=end) {
while(array[start]<pivot) start++;
while(array[end]>pivot) end--;
if(start<=end) {
int tmp = array[start];
array[start]=array[end];
array[end]=tmp;
start++;
end--;
}
}
return start;
}
static int[] quickSort(int[] array,int start, int end) {
int p = partition(array, start, end);
if(start<p-1)
quickSort(array,start,p-1);
if(p<end)
quickSort(array,p,end);
return array;
}
public static void main(String[] args) {
int[] array = {4,2,3,5,9};
array = quickSort(array,0,array.length-1);
System.out.println(Arrays.toString(array));
}
}
출처 : https://heekim0719.tistory.com/282

출처 : https://ko.wikipedia.org/wiki/%ED%9E%99_%EC%A0%95%EB%A0%AC
import java.util.PriorityQueue;
public class Heap_Sort {
public static void main(String[] args) {
int[] arr = {3, 7, 5, 4, 2, 8};
System.out.print(" 정렬 전 original 배열 : ");
for(int val : arr) {
System.out.print(val + " ");
}
PriorityQueue<Integer> heap = new PriorityQueue<Integer>();
// 배열을 힙으로 만든다.
for(int i = 0; i < arr.length; i++) {
heap.add(arr[i]);
}
// 힙에서 우선순위가 가장 높은 원소(root노드)를 하나씩 뽑는다.
for(int i = 0; i < arr.length; i++) {
arr[i] = heap.poll();
}
System.out.print("\n 정렬 후 배열 : ");
for(int val : arr) {
System.out.print(val + " ");
}
}
}