-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuickSort.java
More file actions
41 lines (36 loc) · 1.06 KB
/
QuickSort.java
File metadata and controls
41 lines (36 loc) · 1.06 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
package sort;
import static sort.Utility.swap;
/**
* 快速排序
*/
public class QuickSort {
public void solution(int[] nums) {
qSort(nums, 0, nums.length - 1);
}
private void qSort(int[] nums, int low, int high) {
if (low < high) {
int partition = getPartition(nums, low, high);
qSort(nums, low, partition - 1);
qSort(nums, partition + 1, high);
}
}
private int getPartition(int[] nums, int low, int high) {
int partition = nums[low]; // 关键字
while (low < high) {
while (low < high && nums[high] >= partition) {
high--;
}
swap(nums, low, high);
while (low < high && nums[low] <= partition) {
low++;
}
swap(nums, low, high);
}
return low;
}
public static void main(String[] args) {
TestProxy proxy = new TestProxy(new QuickSort());
QuickSort sort = (QuickSort) proxy.newInstance();
sort.solution(Utility.randomArr());
}
}