-
Notifications
You must be signed in to change notification settings - Fork 1
/
912+Sort an Array.cpp
50 lines (38 loc) · 1.1 KB
/
912+Sort an Array.cpp
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
class Solution {
public:
void quickSort(vector<int>& nums, int lo, int hi) {
if (lo >= hi) return;
int p = partition(nums, lo, hi);
quickSort(nums, lo, p - 1);
quickSort(nums, p + 1, hi);
}
int partition(vector<int>& nums, int lo, int hi) {
int pivot = nums[lo];
int i = lo + 1, j = hi;
while (i <= j) {
while (j > lo && nums[j] >= pivot) j--;
while (i < hi && nums[i] < pivot) i++;
if (i >= j) break;
swap(nums, i, j);
}
swap(nums, lo, j);
return j;
}
void swap(vector<int>& nums, int x, int y) {
int tmp = nums[x];
nums[x] = nums[y];
nums[y] = tmp;
}
void shuffle(vector<int>& nums) {
int n = nums.size();
for (int i = 0; i < n; ++i) {
int r = i + rand() % (n - i);
swap(nums, i, r);
}
}
vector<int> sortArray(vector<int>& nums) {
shuffle(nums);
quickSort(nums, 0, nums.size() - 1);
return nums;
}
};