We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
2 parents ff7a256 + 7fbeb96 commit 5f68badCopy full SHA for 5f68bad
Arrays-Sorting/src/Counting Sort/counting_sort.py
@@ -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