-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathQuickSort.java
48 lines (42 loc) · 1.2 KB
/
QuickSort.java
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
package Sort;
/**
快速排序
时间复杂度 平均o(nlogn) 最好o(nlogn) 最坏o(n^2)
空间复杂度 o(nlogn)
不稳定排序
**/
public class QuickSort {
public static int partition(Integer[] data, int low, int high) {
int pivotKey = (low + high) / 2;
int pivot = data[pivotKey];
data[pivotKey] = data[low];
while (low < high) {
while (low < high && data[high] >= pivot) {
high--;
}
data[low] = data[high];
while (low < high && data[low] <= pivot) {
low++;
}
data[high] = data[low];
}
data[low] = pivot;
return low;
}
public static void qSort(Integer[] data, int low, int high) {
if (low >= high) {
return;
}
int pivot = partition(data, low, high);
qSort(data, low, pivot - 1);
qSort(data, pivot + 1, high);
}
public static void quickSort(Integer[] data) {
qSort(data, 0, data.length - 1);
}
public static void main(String[] args) {
Integer[] c = {23, 4, 9, 23, 1, 45, 27, 5, 2};
quickSort(c);
Stream.of(c).forEach(System.out::println);
}
}