forked from rishabhgarg25699/Competitive-Programming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCountSort.py
More file actions
24 lines (23 loc) · 728 Bytes
/
CountSort.py
File metadata and controls
24 lines (23 loc) · 728 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
#Implementation of count sort(O(n))
def countsort(A,B,C,k):
for i in range(k):
C[i]=0
for q in range(len(A)):
C[A[q]]=C[A[q]]+1
#C[i] contains no. of elemeents equal to i
for p in range(k):
if p==0:
continue
else:
C[p]=C[p]+C[p-1]
#C[i]now contains the number of elemnmtss less than or equal to i
for j in range(len(A)):
B[C[A[j]]-1] = A[j]
C[A[j]] = C[A[j]]-1
#Driver Code
n=int(input("Enter range: ")) #Taking the range as input
A=[int(x) for x in input(('Enter whole numbers less than %d :'%(n))).split()]#Inputting the sequence to be sorted
B=[0]*len(A)
C=[0]*n
countsort(A,B,C,n)
print(B)#Outputting