forked from MakeContributions/DSA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
count-sort.py
28 lines (25 loc) · 827 Bytes
/
count-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
#Code in python for count sort
def count_sort(a):
max_element = int(max(a))
min_element = int(min(a))
range_of_elements = max_element - min_element + 1
# Create a count array to store count of individual
# elements and initialize count array as 0
c= [0 for _ in range(range_of_elements)]
o= [0 for _ in range(len(a))]
for i in range(0, len(a)):
c[a[i]-min_element] += 1
for i in range(1, len(c)):
c[i] += c[i-1]
for i in range(len(a)-1, -1, -1):
o[c[a[i] - min_element] - 1] = a[i]
c[a[i] - min_element] -= 1
for i in range(0, len(a)):
a[i] = o[i]
return a
a=[-1,-2,6,5,10]
ans = count_sort(a)
print("Sorted array is ")
print(ans)
#TIME COMPLEXITY
#O(n+k) where n is the number of elements in input array and k is the range of input