-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQuick3way.java
More file actions
40 lines (35 loc) · 918 Bytes
/
Copy pathMyQuick3way.java
File metadata and controls
40 lines (35 loc) · 918 Bytes
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
package com.clay;
/**
* MyQuick3way
*/
public class MyQuick3way {
public static void sort(Comparable[] aComparables) {
sort(aComparables, 0, aComparables.length - 1);
}
private static void sort(Comparable[] a, int lo, int hi) {
int lt = lo, i = lo + 1, gt = hi;
Comparable v = a[lo];
while (i <= gt) {
int cmp = a[i].compareTo(v);
if (cmp < 0) {
exch(a, lt++, i++);
}
else if (cmp > 0) {
exch(a, i, gt--);
}
else {
i++;
}
}
sort(a, lo, lt - 1);
sort(a, gt + 1, hi);
}
private static boolean less(Comparable a, Comparable b) {
return a.compareTo(b) < 0;
}
private static void exch(Comparable[] a, int i, int j) {
Comparable v = a[i];
a[i] = a[j];
a[j] = v;
}
}