-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadixSort.py
More file actions
26 lines (23 loc) · 829 Bytes
/
RadixSort.py
File metadata and controls
26 lines (23 loc) · 829 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
def radix_sort(to_be_sorted):
maximum_value = max(to_be_sorted)
max_exponent = len(str(maximum_value))
being_sorted = to_be_sorted[:]
for exponent in range(max_exponent):
position = exponent + 1
index = -position
digits = [[] for items in range(10)]
for num in being_sorted:
str_num = str(num)
try:
digit = str_num[index]
except IndexError:
digit = 0
digit = int(digit)
digits[digit].append(num)
being_sorted = []
for numeral in digits:
being_sorted.extend(numeral)
return being_sorted
unsorted_list = [830, 921, 163, 373, 961, 559, 89, 199, 535, 959, 40, 641, 355, 689, 621, 183, 182, 524, 1]
ordered_list = radix_sort(unsorted_list)
print(ordered_list)