-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy path912.cpp
More file actions
26 lines (24 loc) · 674 Bytes
/
Copy path912.cpp
File metadata and controls
26 lines (24 loc) · 674 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
class Solution {
public:
vector<int> sortArray(vector<int> &nums) {
QuickSort(nums, 0, nums.size());
return nums;
}
template<typename T>
void QuickSort(vector<T> &nums, int x, int y) {
if (x >= y - 1)
return;
int pivot = nums[x], i = x, j = y - 1;
while (i < j) {
while (i < j && nums[j] >= pivot)
--j;
while (i < j && nums[i] <= pivot)
++i;
swap(nums[i], nums[j]);
if (i == j && nums[i] < nums[x])
swap(nums[x], nums[i]);
}
QuickSort(nums, x, i);
QuickSort(nums, i + 1, y);
}
};