-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path11. Sort 0s, 1s and 2s
64 lines (55 loc) · 1.25 KB
/
11. Sort 0s, 1s and 2s
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
void sort012(vector<int>& a) {
// code here
sort(a.begin(), a.end());
}
};
//{ Driver Code Starts.
int main() {
int t;
cin >> t;
cin.ignore();
while (t--) {
vector<int> a;
string input;
int num;
getline(cin, input);
stringstream s2(input);
while (s2 >> num) {
a.push_back(num);
}
Solution ob;
ob.sort012(a);
int n = a.size();
for (int i = 0; i < n; i++) {
cout << a[i] << " ";
}
cout << endl;
cout << "~" << endl;
}
return 0;
}
// } Driver Code Ends
=========================================================================
void sortArray(vector<int> nums) {
int left= 0, mid = 0, right= nums.size()-1;
while (mid <= right) {
if (nums[mid] == 0) {
swap(nums[mid], nums[left]);
left++;
mid++;
}
else if (nums[mid] == 1) {
mid++;
}
else if (nums[mid] == 2) {
swap(nums[mid], nums[right]);
right--;
}
}
}