-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort colors.cpp
More file actions
35 lines (30 loc) · 792 Bytes
/
sort colors.cpp
File metadata and controls
35 lines (30 loc) · 792 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
30
31
32
33
34
35
quicksort - inplace
class Solution {
public:
void sortColors(vector<int>& nums) {
if(nums.size()==1 ||nums.size()== 0)
return;
int start = 0;
int end = nums.size()-1;
int index = 0 ;
while(start<end && index<=end)
{
if(nums[index]== 0)
{
nums[index] = nums[start];
nums[start] = 0;
start++;
index++;
}
else if(nums[index]==2)
{
nums[index] = nums[end];
nums[end] = 2;
end--;
}
else{
index++;
}
}
}
};