1. Merge Sort(합병정렬)
👩💻 구현
package Algorithm;
public class merge_sort {
private static int[] input = {5, 6, 2, 8, 7, 23, 4, 1};
static int[] tmp = new int[input.length];
public static void main(String[] args) {
mergeSort(0, input.length-1);
for(int i : input) {
System.out.print(i + " ");
}
}
public static void mergeSort(int start, int end) {
if(start < end) {
int mid = (start + end) / 2;
mergeSort(start, mid);
mergeSort(mid+1, end);
int p = start;
int q = mid + 1;
int idx = p;
while(p <= mid || q <= end) {
if(q > end || (p <= mid && input[p] <= input[q])) {
tmp[idx++] = input[p++];
}else {
tmp[idx++] = input[q++];
}
}
for(int i=start; i<=end; i++) {
input[i] = tmp[i];
}
}
}
}
https://ict-nroo.tistory.com/53?category=698685