-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlk912_Quick.java
61 lines (54 loc) · 1.53 KB
/
lk912_Quick.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
49
50
51
52
53
54
55
56
57
58
59
60
61
class Solution {
public int[] sortArray(int[] nums) {
Quick.sort(nums);
return nums;
}
}
class Quick {
public static void sort(int[] nums) {
shuffle(nums);
sort(nums, 0, nums.length - 1);
}
private static void sort(int[] nums, int lo, int hi) {
if (lo >= hi) {
return;
}
int p = partition(nums, lo, hi);
sort(nums, lo, p - 1);
sort(nums, p+1, hi);
}
private static int partition(int[] nums, int lo, int hi) {
int pivot = nums[lo];
//关于区间的边界控制需要格外小心,这里把i,j定义为开区间,同时定义:[lo,i) <= pivot,(j,hi] > pivot,维护它
int i = lo + 1, j = hi;
//i>j时结束循环,以保证区间[lo,hi]都被覆盖
while (i <= j) {
while (i < hi && nums[i] <= pivot) {
i++;
}
while (j > lo && nums[j] > pivot) {
j--;
}
if (i >= j) {
break;
}
swap(nums, i, j);
}
//将pivot交换过去
swap(nums, lo, j);
return j;
}
private static void shuffle(int[] nums) {
Random rand = new Random();
int n = nums.length;
for (int i = 0; i < n; i++) {
int r = i + rand.nextInt(n - i);
swap(nums, i, r);
}
}
private static void swap(int[] nums, int i, int j) {
int temp = nums[i];
nums[i] = nums[j];
nums[j] = temp;
}
}