forked from ccheckmate/PythonAlgorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathc.cpp
More file actions
29 lines (28 loc) · 670 Bytes
/
Copy pathc.cpp
File metadata and controls
29 lines (28 loc) · 670 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
27
28
29
class Solution {
public:
int partition(int l,int r,vector<int>& nums)
{
int i=l;
for(int j=l;j<r;j++)
{
if(nums[j]<nums[r])
swap(nums[i++],nums[j]);
}
swap(nums[i],nums[r]);
return i;
}
void quickSort(int l,int r,vector<int>& nums)
{
if(l>=r)
return;
int pivot=l+(rand()%(r-l+1));
swap(nums[pivot],nums[r]);
pivot=partition(l,r,nums);
quickSort(l,pivot-1,nums);
quickSort(pivot+1,r,nums);
}
vector<int> sortArray(vector<int>& nums) {
quickSort(0,nums.size()-1,nums);
return nums;
}
};