-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSort_Array_of_012.cpp
64 lines (54 loc) · 1.01 KB
/
Sort_Array_of_012.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
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
#include <bits/stdc++.h>
#include <iostream>
using namespace std;
void sort012(int arr[], int n) {
int low = 0;
int mid = 0;
int high = n - 1;
while (mid <= high) {
if (arr[mid] == 0) {
swap(arr[mid], arr[low]);
mid++;
low++;
} else if (arr[mid] == 1) {
mid++;
} else {
swap(arr[mid], arr[high]);
high--;
}
}
}
int main() {
int t;
cin >> t;
while (t > 0) {
int n;
cin >> n;
int arr[n];
for (int i = 0; i < n; i++) {
cin >> arr[i];
}
sort012(arr, n);
for (int i = 0; i < n; i++) {
cout << arr[i] << " ";
}
cout << endl;
t--;
}
return 0;
}
/*
Example:
Input =
1 <= Number of Tests
6 <= Length of Array
2 1 0 1 2 0 <= Array values
Output =
0 0 1 1 2 2 <= Sorted Array by Min > Max
Input 2 =
1
11
2 2 1 1 2 1 0 0 1 2 2
Output 2 =
0 0 1 1 1 1 2 2 2 2 2
*/