forked from DSC-COEA-Ambajogai/Hacktoberfest2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSort an array of 0s, 1s and 2s.py
67 lines (44 loc) · 1.23 KB
/
Sort an array of 0s, 1s and 2s.py
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
""" Given an array A of size N containing 0s, 1s, and 2s; you need to sort the array in ascending order.
Input:
The first line contains an integer 'T' denoting the total number of test cases. Then T testcases follow. Each testcases contains two lines of input. The first line denotes the size of the array N. The second lines contains the elements of the array A separated by spaces.
Output:
For each testcase, print the sorted array.
Constraints:
1 <= T <= 500
1 <= N <= 106
0 <= Ai <= 2
Example:
Input :
2
5
0 2 1 2 0
3
0 1 0
Output:
0 0 1 2 2
0 0 1
Explanation:
Testcase 1: After segragating the 0s, 1s and 2s, we have 0 0 1 2 2 which shown in the output. """
T = int(input())
for i in range(0,T):
N = int(input())
c1 = 0
c2 = 0
c3 = 0
lst = [int(item) for item in input().split() ]
new_lst = []
for i in range(0,N):
if lst[i]==0:
c1 += 1
elif lst[i] == 1:
c2 += 1
else:
c3 +=1
""" print("0:",c1,",", "1:",c2, ",", "2:",c3) """
for i in range(0,c1):
new_lst.append(0)
for i in range(0,c2):
new_lst.append(1)
for i in range(c3):
new_lst.append(2)
print("Final : " ,new_lst)