forked from OmkarPathak/pygorithm
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradix_sort.py
More file actions
38 lines (31 loc) · 801 Bytes
/
radix_sort.py
File metadata and controls
38 lines (31 loc) · 801 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
32
33
34
35
36
37
38
"""
Author: Ian Doarn
Date: 31st Oct 2017
Reference:
https://stackoverflow.com/questions/35419229/python-radix-sort
"""
def sort(_list, base=10):
"""
Radix Sort
:param _list: array to sort
:param base: base radix number
:return: sorted list
"""
# TODO: comment this
result_list = []
power = 0
while _list:
bs = [[] for _ in range(base)]
for x in _list:
bs[x // base ** power % base].append(x)
_list = []
for b in bs:
for x in b:
if x < base ** (power + 1):
result_list.append(x)
else:
_list.append(x)
power += 1
return result_list
if __name__ == '__main__':
print(sort([170, 45, 75, 90, 802, 24, 2, 66]))