forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcounting_sort.py
More file actions
58 lines (44 loc) · 1.14 KB
/
counting_sort.py
File metadata and controls
58 lines (44 loc) · 1.14 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
"""
Author: OMKAR PATHAK
Created On: 31st July 2017
- Best = Average = Worst = O(n + k)
"""
import inspect
# counting sort algorithm
def sort(_list):
"""
counting sort algorithm
:param _list: list of values to sort
:return: sorted values
"""
try:
max_value = 0
for i in range(len(_list)):
if _list[i] > max_value:
max_value = _list[i]
buckets = [0] * (max_value + 1)
for i in _list:
buckets[i] += 1
i = 0
for j in range(max_value + 1):
for a in range(buckets[j]):
_list[i] = j
i += 1
return _list
except TypeError as error:
print('Counting Sort can only be applied to integers. {}'.format(error))
# TODO: Are these necessary?
def time_complexities():
"""
Return information on functions
time complexity
:return: string
"""
return "Best Case: O(n + k), Average Case: O(n + k), Worst Case: O(n + k)"
def get_code():
"""
easily retrieve the source code
of the sort function
:return: source code
"""
return inspect.getsource(sort)