-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSort.java
More file actions
42 lines (36 loc) · 1.12 KB
/
MergeSort.java
File metadata and controls
42 lines (36 loc) · 1.12 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
39
40
41
42
import java.util.Comparator;
public class MergeSort {
private static long comparisons = 0;
public static <K> void sort(K[] S, Comparator<K> comp) {
if (S.length < 2) return;
// Divide
int mid = S.length / 2;
K[] S1 = (K[]) new Object[mid];
K[] S2 = (K[]) new Object[S.length - mid];
System.arraycopy(S, 0, S1, 0, mid);
System.arraycopy(S, mid, S2, 0, S.length - mid);
sort(S1, comp);
sort(S2, comp);
merge(S1, S2, S, comp);
}
public static <K> void merge(K[] S1, K[] S2, K[] S, Comparator<K> comp) {
int i = 0, j = 0;
while (i + j < S.length) {
if (j == S2.length) {
S[i + j] = S1[i++];
} else if (i == S1.length) {
S[i + j] = S2[j++];
} else {
comparisons++;
if (comp.compare(S1[i], S2[j]) < 0) {
S[i + j] = S1[i++];
} else {
S[i + j] = S2[j++];
}
}
}
}
public static long getComparisons() {
return comparisons;
}
}