forked from thuva4/Algorithms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_sort.py
More file actions
31 lines (25 loc) · 810 Bytes
/
Copy pathcounting_sort.py
File metadata and controls
31 lines (25 loc) · 810 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
# Python3 implementation for counting sort
def counting_sort(arr):
m = max(arr) # get the max item in the array to set the index and outputs
count = [0] * (m+1)
output = [0] * (m+1)
# store counts into array.
for x in arr:
# raise an error if array has non-integers
if isinstance(x, int):
count[x] += 1
else:
raise TypeError("Invalid item in array. It should be an integer! {}".format(x))
# update count to store index
total = 0
for x in range(len(count)):
temp = count[x]
count[x] = total
total += temp
# update the output based on the counts
for x in arr:
output[count[x]] = x
# increment the index
count[x] += 1
return output[:len(arr)] # only return values that are updated.
print(counting_sort([1,1,4,2,2,2,3,5,230,9]))