-
Notifications
You must be signed in to change notification settings - Fork 28
/
MergeSortDemo.java
76 lines (63 loc) · 1.74 KB
/
MergeSortDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package ds_005_sorting;
/*
* https://www.cs.cmu.edu/~adamchik/15-121/lectures/Sorting%20Algorithms/code/MergeSort.java
*/
public class MergeSortDemo {
public static void main(String[] args) {
int[] array = {4, 5, 9, 8, 1, 2, 7, 6, 3 };
printArray(array);
mergeSort(array);
printArray(array);
}
private static void mergeSort(int[] array) {
int[] temp = new int[array.length];
mergeSort(array, temp, 0, array.length-1);
}
private static void mergeSort(int[] array, int[] temp, int left, int right) {
if(left < right) {
int center = (left + right) / 2;
mergeSort(array, temp, left, center);
mergeSort(array, temp, center + 1, right);
merge(array, temp, left, center + 1, right);
}
}
private static void merge(int[] array, int[] temp, int left, int right, int rightEnd) {
int leftEnd = right-1;
int k = left;
int num = rightEnd - left + 1;
while(left <= leftEnd && right <= rightEnd) {
if(array[left] <= array[right]) {
temp[k++] = array[left++];
} else {
temp[k++] = array[right++];
}
}
// copy rest of first half
while(left <= leftEnd) {
temp[k++] = array[left++];
}
// copy rest of right half
while(right <= rightEnd) {
temp[k++] = array[right++];
}
// copy temp back
for(int i = 0; i < num; i++, rightEnd--) {
array[rightEnd] = temp[rightEnd];
}
}
// trivial
private static void printArray(int[] array) {
System.out.print("Array: ");
for(int i = 0; i < array.length; i++) {
System.out.printf("%d ", array[i]);
}
System.out.print("\n");
}
private static void printArray(int[] array, int begin, int end) {
System.out.print("Array: ");
for(int i = begin; i < end; i++) {
System.out.printf("%d ", array[i]);
}
System.out.print("\n");
}
}