-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuick.java
More file actions
58 lines (45 loc) · 1.35 KB
/
Quick.java
File metadata and controls
58 lines (45 loc) · 1.35 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
52
53
54
55
56
57
58
import java.util.Random;
public class Quick {
public static void sort(int[] a) {
// shuffle the array to prevent worst case O(n^2)
shuffle(a);
sort(a, 0, a.length-1);
}
private static void sort(int[] a, int lo, int hi) {
if (lo >= hi) return;
int pi = partition(a, lo, hi); // partition index
sort(a, lo, pi-1);
sort(a, pi+1, hi);
}
private static int partition(int[] a, int lo, int hi) {
int i = lo, j = hi+1;
int pivot = a[lo];
while (true) {
while (a[++i] < pivot) if (i == hi) break; // scan left
while (a[--j] > pivot) if (j == lo) break; // scan right
if (i >= j) break;
exch(a, i, j);
}
exch(a, lo, j);
return j;
}
// uniformly random shuffle with the Fisher–Yates Algorithm
private static void shuffle(int[] a) {
Random rand = new Random();
for (int i = a.length-1; i > 0; i--) {
exch(a, i, rand.nextInt(i+1));
}
}
private static void exch(int[] a, int i, int j) {
int t = a[i];
a[i] = a[j];
a[j] = t;
}
public static void main(String[] args) {
int[] a = new int[] {4, 5, 2, 3, 1, 7, 9, 10};
sort(a);
for (int v: a) {
System.out.println(v);
}
}
}