-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathSortColor.cpp
37 lines (34 loc) · 940 Bytes
/
SortColor.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
class Solution {
public:
//DUTCH NATIONAL FLAG ALGORITHM
void sortColors(vector<int>& nums) {
int lo=0, mid=0, hi=nums.size()-1;
while(mid<=hi){
if(nums[mid]==0){
swap(nums[lo], nums[mid]);
lo++, mid++;
}else if(nums[mid]==1){
mid++;
}else{
swap(nums[mid], nums[hi]);
hi--;
}
}
}
/* void sortColors(vector<int>& nums) {
int lo=0, mid=0, hi=nums.size()-1;
while(mid<=hi){
switch(nums[mid]){
case 0:
swap(nums[lo++], nums[mid++]);
break;
case 1:
mid++;
break;
case 2:
swap(nums[mid], nums[hi--]);
break;
}
}
} */
};