-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRadix_sort
More file actions
31 lines (23 loc) · 884 Bytes
/
Radix_sort
File metadata and controls
31 lines (23 loc) · 884 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
# sorts integers based on the number in each digit of the overall number. It places the digits into buckets and returns the list sorted
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 i in range(10)]
for number in being_sorted:
number_as_a_string = str(number)
try:
digit = number_as_a_string[index]
except IndexError:
digit = 0
digit = int(digit)
digits[digit].append(number)
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]
print(radix_sort(unsorted_list))