-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMergeBU.java
50 lines (44 loc) · 1.41 KB
/
MergeBU.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
public class MergeBU {
private static boolean less(Comparable a, Comparable b) {
return a.compareTo(b) < 0;
}
private static void merge(Comparable[] a, Comparable[] aux, int low, int mid, int high) {
for (int k = 0; k <= high; ++k) {
aux[k] = a[k];
}
int i = low;
int j = mid + 1;
for (int k = low; k <= high; ++k) {
if (i > mid) {
a[k] = aux[j++];
} else if (j > high) {
a[k] = aux[i++];
} else if (less(a[j], a[i])) {
a[k] = aux[j++];
} else {
a[k] = aux[i++];
}
}
}
public static void sort(Comparable[] a) {
Comparable[] aux = new Comparable[a.length];
int N = a.length;
for (int size = 1; size < N; size = size + size) {
for (int low = 0; low < N - size; low = low + size + size) {
merge(a, aux, low, low - size + 1, Math.min(low + size + size - 1, N - 1));
}
}
}
public static void print(Object[] array) {
for (Object a: array) {
System.out.print(a + " ");
}
System.out.println("");
}
public static void main(String[] args) {
String[] array = {"Hope", "Eve", "Alice", "Charlie", "Tom", "Bob"};
Merge.print(array);
Merge.sort(array);
Merge.print(array);
}
}