-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathQuick.java
63 lines (55 loc) · 1.51 KB
/
Quick.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
55
56
57
58
59
60
61
62
63
public class Quick {
private static boolean less(Comparable a, Comparable b) {
return a.compareTo(b) < 0;
}
private static void exchange(Comparable[] a, int i, int j) {
Comparable temp = a[i];
a[i] = a[j];
a[j] = temp;
}
private static int partition(Comparable[] a, int low, int high) {
int i = low;
int j = high + 1;
while (true) {
while (less(a[i++], a[low])) {
if (i == high) {
break;
}
}
while (less(a[low], a[--j])) {
if (j == low) {
break;
}
}
if (j <= i) {
break;
}
exchange(a, i, j);
}
exchange(a, low, j);
return j;
}
private static void sort(Comparable[] a, int low, int high) {
if (low >= high) {
return;
}
int mid = partition(a, low, high);
sort(a, low, mid - 1);
sort(a, mid + 1, high);
}
private static void sort(Comparable[] a) {
sort(a, 0, a.length-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 = {"Eve", "Charlie", "Bob", "Hope", "Alice", "Tom"};
Quick.print(array);
Quick.sort(array);
Quick.print(array);
}
}