Skip to content

Commit fab7683

Browse files
committed
added count sort
1 parent b56cb26 commit fab7683

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

sorts/count_sort.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
from __future__ import print_function
2+
3+
4+
def count_sort(a):
5+
aux=[]
6+
max1=max(a)
7+
for i in range(0,max1+1):
8+
aux.append(0)
9+
for x in a:
10+
aux[x]+=1
11+
for i in range(1,max1+1):
12+
aux[i]=aux[i]+aux[i-1]
13+
op=[]
14+
for i in range(0,len(a)):
15+
op.append(0)
16+
for i in range(len(a)-1,-1,-1):
17+
op[aux[a[i]]-1]=a[i]
18+
aux[a[i]]=aux[a[i]]-1
19+
return op
20+
21+
22+
if __name__ == '__main__':
23+
import sys
24+
# For python 2.x and 3.x compatibility: 3.x has not raw_input builtin
25+
# otherwise 2.x's input builtin function is too "smart"
26+
if sys.version_info.major < 3:
27+
input_function = raw_input
28+
else:
29+
input_function = input
30+
31+
user_input = input_function('Enter numbers separated by a comma (no negative numbers):\n')
32+
unsorted = [int(item) for item in user_input.split(',')]
33+
print(count_sort(unsorted))

0 commit comments

Comments
 (0)