-
Notifications
You must be signed in to change notification settings - Fork 0
/
counting_sort.py
50 lines (22 loc) · 992 Bytes
/
counting_sort.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
def counting_sort(collection):
if collection==[]:
return []
coll_len=len(collection)
coll_max=max(collection)
coll_min=min(collection)
counting_arr_length=coll_max + 1 - coll_min
counting_arr = [0] * counting_arr_length
for number in collection:
counting_arr[number-coll_min] += 1
for i in range(1,counting_arr_length):
counting_arr[i] = counting_arr[i] + counting_arr[i-1]
ordered=[0] * coll_len
for i in reversed(range(0,coll_len)):
ordered[counting_arr[collection[i] - coll_min] - 1] = collection[i]
counting_arr[collection[i] - coll_min] -= 1
return ordered
if __name__=="__main__":
user_input=input("Enter numbers separated by a comma: \n").strip()
unsorted=[int(item) for item in user_input.split(",")]
x=counting_sort(unsorted)
print(x)