-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMergesort.java
49 lines (40 loc) · 1.14 KB
/
Mergesort.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
import java.util.Arrays;
public class Mergesort {
public static void main(String[] args) {
int[] values = {6, 1, 2, 3, 4, 4, 0};
mergeSort(values);
for (int i : values) {
System.out.print(i + " ");
}
System.out.println();
}
public static void merge(int[] left, int[] right, int[] data) {
int i = 0;
int j = 0;
int k = 0;
while (i < left.length && j < right.length) {
if (left[i] <= right[j]) {
data[k++] = left[i++];
} else {
data[k++] = right[j++];
}
}
// fill in remaining values
while (i < left.length) {
data[k++] = left[i++];
}
while (j < right.length) {
data[k++] = right[j++];
}
}
public static void mergeSort(int[] data) {
if (data.length < 2) {
return;
}
int[] left = Arrays.copyOf(data, data.length / 2);
int[] right = Arrays.copyOfRange(data, left.length, data.length);
mergeSort(left);
mergeSort(right);
merge(left, right, data);
}
}