forked from tanus786/CP-Codes-HackOctober-Fest-2023
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsort_0_1_2.cpp
More file actions
60 lines (52 loc) · 948 Bytes
/
sort_0_1_2.cpp
File metadata and controls
60 lines (52 loc) · 948 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
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
#include <iostream>
#include <algorithm>
using namespace std;
void sort012(int *arr, int n)
{
int z = 0, l = n - 1;
for (int i = 0; i <= l;)
{
if (arr[i] == 0)
{
int temp = arr[z];
arr[z] = arr[i];
arr[i] = temp;
z++;
i++;
}
else if (arr[i] == 2)
{
int temp = arr[l];
arr[l] = arr[i];
arr[i] = temp;
l--;
}
else
{
i++;
}
}
}
int main()
{
int t;
cin >> t;
while (t--)
{
int size;
cin >> size;
int *arr = new int[size];
for (int i = 0; i < size; i++)
{
cin >> arr[i];
}
sort012(arr, size);
for (int i = 0; i < size; i++)
{
cout << arr[i] << " ";
}
delete[] arr;
cout << endl;
}
return 0;
}