forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
38 lines (33 loc) · 1.01 KB
/
MergeSort.java
File metadata and controls
38 lines (33 loc) · 1.01 KB
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
public class MergeSort {
public static void sort(int[] a) {
int[] helper = new int[a.length];
sort(a, 0, a.length - 1, helper);
}
public static void sort(int[] a, int low, int high, int[] helper) {
if (low >= high) {
return;
}
int middle = low + (high - low) / 2;
sort(a, low, middle, helper);
sort(a, middle + 1, high, helper);
merge(a, low, middle, high, helper);
}
public static void merge(int[] a, int low, int middle, int high, int[] helper) {
for (int i = low; i <= high; i++) {
helper[i] = a[i];
}
int i = low;
int j = middle + 1;
for (int k = low; k <= high; k++) {
if (i > middle) {
a[k] = helper[j++];
} else if (j > high) {
a[k] = helper[i++];
} else if (helper[i] <= helper[j]) {
a[k] = helper[i++];
} else {
a[k] = helper[j++];
}
}
}
}