-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathQuick.java
More file actions
51 lines (44 loc) · 1.07 KB
/
Copy pathQuick.java
File metadata and controls
51 lines (44 loc) · 1.07 KB
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
public class Quick extends SortingHelpers
{
// Quick sort
public static void sort(Comparable[] a)
{
// randomly shuffle the array for probablistic guarantee
Knuth.shuffle(a);
// no auxiliary array is needed here
sort(a, 0, a.length-1);
}
private static int partition(Comparable[] a, int lo, int hi)
{
// choose a[lo] as partition element
int i = lo, j = hi+1;
while (true)
{
// increment i as long as a[i] < a[lo]
while (less(a[++i], a[lo]))
if (i == hi) break;
// decrement j as long as a[j] > a[lo]
while (less(a[lo], a[--j]))
if (j == lo) break;
// check if pointers cross
if (i >= j) break;
// swap pointer values
exch(a, i, j);
}
// j now points to correct location of partition element
exch(a, lo, j);
return j;
}
private static void sort(Comparable[] a, int lo, int hi)
{
// recursive routine
if (hi <= lo) return;
int CUTOFF = 10;
if (hi <= lo + CUTOFF - 1)
Insertion.sort(a, lo, hi);
int r = partition(a, lo, hi);
// r is already in correct location
sort(a, lo, r-1);
sort(a, r+1, hi);
}
}