-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyQuick3way2.java
More file actions
39 lines (35 loc) · 853 Bytes
/
Copy pathMyQuick3way2.java
File metadata and controls
39 lines (35 loc) · 853 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
package com.clay;
/**
* MyQuick3way2
*/
public class MyQuick3way2 {
public static void sort(Comparable[] a) {
sort(a, 0, a.length - 1);
}
private static void sort(Comparable[] a, int lo, int hi) {
if (hi <= lo + 10) {
Insertion.sort(a);
}
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 void exch(Comparable[] a, int i, int j) {
Comparable v = a[i];
a[i] = a[j];
a[j] = v;
}
}