-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathEx_2_2_01.java
54 lines (47 loc) · 1.33 KB
/
Ex_2_2_01.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
package Ch_2_2;
import tools.PrintUtil;
/**
* Created by HuGuodong on 2022/3/5.
*/
public class Ex_2_2_01 {
public static Comparable<Character>[] aux;
public static Comparable<Character>[] input;
public static void main(String[] args) {
input = new Comparable[]{'A', 'E', 'Q', 'S', 'U', 'Y', 'E', 'I', 'N', 'O', 'S', 'T'};
aux = new Comparable[input.length];
merge(input, 0, 5, 11);
System.out.printf("Answer is: ");
PrintUtil.show(input);
}
public static void merge(Comparable[] a, int lo, int mid, int hi) {
int i = lo, j = mid + 1;
for (int k = 0; k <= hi; k++) {
aux[k] = a[k];
}
System.out.printf("%s ", "input: k");
PrintUtil.show(input, 0, input.length, true);
System.out.printf("%5s %5s\n", "i", "j");
for (int k = lo; k <= hi; k++) {
System.out.printf("%8s ", k);
PrintUtil.show(input, 0, k, true);
System.out.printf("%5s %5s | ", i, j);
PrintUtil.show(aux, i, mid, true);
PrintUtil.show(aux, j, aux.length, false);
if (i > mid) {
a[k] = aux[j++];
}
else if (j > hi) {
a[k] = aux[i++];
}
else if (less(aux[i], aux[j])) {
a[k] = aux[i++];
}
else {
a[k] = aux[j++];
}
}
}
public static boolean less(Comparable a, Comparable b) {
return a.compareTo(b) < 0;
}
}