forked from souravjain540/Basic-Python-Programs
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcounting_sort.py
More file actions
41 lines (18 loc) · 662 Bytes
/
Copy pathcounting_sort.py
File metadata and controls
41 lines (18 loc) · 662 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
def countSort(array):
output=[0 for i in range(len(array))]
count=[0 for i in range(256)]
ans = ["" for i in array]
for i in array:
count[ord(i)] += 1
for i in range(256):
count[i]= count[i] + count[i-1]
for i in range(len(array)):
output[count[ord(array[i])] -1] = array[i]
count[ord(array[i])] -= 1
for i in range(len(array)):
ans[i] = output[i]
return ans
if __name__=="__main__":
array="retributivejustice"
ans=countSort(array)
print("Sorted character array is %s" % ("".join(ans)))