-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSort_0s_1s_2s_in_array.cpp
More file actions
69 lines (60 loc) · 1.31 KB
/
Sort_0s_1s_2s_in_array.cpp
File metadata and controls
69 lines (60 loc) · 1.31 KB
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
65
66
67
68
69
//{ Driver Code Starts
#include <bits/stdc++.h>
using namespace std;
// } Driver Code Ends
class Solution {
public:
void sort012(vector<int>& a) {
int n = a.size();
int low = 0;
int mid = 0;
int high = n - 1;
while(mid <= high)
{
if(a[mid] == 0)
{
int temp = a[low];
a[low] = a[mid];
a[mid] = temp;
low++;
mid++;
}
else if(a[mid] == 1)
{
mid++;
}
else
{
int temp = a[high];
a[high] = a[mid];
a[mid] = temp;
high--;
}
}
}
};
//{ 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;
}
return 0;
}
// } Driver Code Ends