-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
42 lines (37 loc) · 1001 Bytes
/
QuickSort.java
File metadata and controls
42 lines (37 loc) · 1001 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
41
42
import java.util.Arrays;
public class QuickSort {
public static void main(String[] args) {
int[] a = { 9, 8, 7, 6, 5, 4, 3, 2, 1 };
sort(a, 0, a.length - 1);
System.out.println(Arrays.toString(a));
}
private static void sort(int[] a, int lo, int hi) {
if (lo >= hi)
return;
int j = partition(a, lo, hi);
sort(a, lo, j);
sort(a, j + 1, hi);
}
private static int partition(int[] a, int lo, int hi) {
int i = lo;
int j = hi + 1;
while (true) {
while (a[++i] < a[lo])
if (i == hi)
break;
while (a[--j] > a[lo])
if (j == lo)
break;
if (i >= j)
break;
swap(a, i, j);
}
swap(a, lo, j);
return j;
}
private static void swap(int[] a, int i, int j) {
int temp = a[i];
a[i] = a[j];
a[j] = temp;
}
}