-
Notifications
You must be signed in to change notification settings - Fork 0
/
Counting sort 2.py
29 lines (24 loc) · 1.59 KB
/
Counting sort 2.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
def countingSort(arr):
cijferlijst = [0 for i in range(100)] # Create list of 100 0's
cijferlijst2 = [] # List for the output
for cijfer in arr: # Increase concerning element with one
cijferlijst[cijfer] += 1
for i in range(len(cijferlijst)): # Create list with cijferlijst
if cijferlijst[i] > 0: # Take element, add element to output-list with value times the index
for j in range(cijferlijst[i]):
cijferlijst2.append(i)
return cijferlijst2
if __name__ == '__main__':
n = int(input())
arr = list(map(int, input().rstrip().split()))
result = countingSort(arr)
print(result)
"""
https://www.hackerrank.com/challenges/countingsort1/problem?h_r=next-challenge&h_v=zen
Input:
100
63 25 73 1 98 73 56 84 86 57 16 83 8 25 81 56 9 53 98 67 99 12 83 89 80 91 39 86 76 85 74 39 25 90 59 10 94 32 44 3 89 30 27 79 46 96 27 32 18 21 92 69 81 40 40 34 68 78 24 87 42 69 23 41 78 22 6 90 99 89 50 30 20 1 43 3 70 95 33 46 44 9 69 48 33 60 65 16 82 67 61 32 21 79 75 75 13 87 70 33
Output:
1 1 3 3 6 8 9 9 10 12 13 16 16 18 20 21 21 22 23 24 25 25 25 27 27 30 30 32 32 32 33 33 33 34 39 39 40 40 41 42 43 44 44 46 46 48 50 53 56 56 57 59 60 61 63 65 67 67 68 69 69 69 70 70 73 73 74 75 75 76 78 78 79 79 80 81 81 82 83 83 84 85 86 86 87 87 89 89 89 90 90 91 92 94 95 96 98 98 99 99
(was:) 0 2 0 2 0 0 1 0 1 2 1 0 1 1 0 0 2 0 1 0 1 2 1 1 1 3 0 2 0 0 2 0 3 3 1 0 0 0 0 2 2 1 1 1 2 0 2 0 1 0 1 0 0 1 0 0 2 1 0 1 1 1 0 1 0 1 0 2 1 3 2 0 0 2 1 2 1 0 2 2 1 2 1 2 1 1 2 2 0 3 2 1 1 0 1 1 1 0 2 2
"""