Skip to content

Commit 5f68bad

Browse files
authored
Merge pull request #25 from OmkarPathak/patch-1
Create counting_sort.py
2 parents ff7a256 + 7fbeb96 commit 5f68bad

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Author: Omkar Pathak
2+
# Time Complexities:
3+
# Best Case: O(n + k),
4+
# Average Case: O(n + k),
5+
# Worst Case: O(n + k)
6+
7+
def sort(_list):
8+
"""
9+
counting sort algorithm
10+
11+
:param _list: list of values to sort
12+
:return: sorted values
13+
"""
14+
try:
15+
max_value = 0
16+
for i in range(len(_list)):
17+
if _list[i] > max_value:
18+
max_value = _list[i]
19+
20+
buckets = [0] * (max_value + 1)
21+
22+
for i in _list:
23+
buckets[i] += 1
24+
i = 0
25+
26+
for j in range(max_value + 1):
27+
for a in range(buckets[j]):
28+
_list[i] = j
29+
i += 1
30+
31+
return _list
32+
33+
except TypeError as error:
34+
print('Counting Sort can only be applied to integers. {}'.format(error))

0 commit comments

Comments
 (0)