-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathradixSort.py
More file actions
54 lines (36 loc) · 1.65 KB
/
radixSort.py
File metadata and controls
54 lines (36 loc) · 1.65 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
[Radix Sort](https://www.codecademy.com/paths/computer-science/tracks/sorting-algorithms/modules/cs-radix-sort/lessons/radix-sort-python/exercises/rendering-the-list)
'''
@created Sept 2th, 2562BE
Radix Sort:
Takes numbers in an input list.
Through each digit in those numbers, from least to most significant.
Looks at the values of those digits.
Buckets the input list according to those digits.
Renders the results from that bucketing.
Repeats this process until the list is sorted.
does! Feel free to play around with the solution code, see if there’s anything you can improve a
'''
def radix_sort(to_be_sorted):
max_exponent = len(str(max(to_be_sorted)))
being_sorted = to_be_sorted[:]
for exponent in range(max_exponent): # the maximum is W exponents
position = exponent + 1
index = -position
# creating the buckets which will be filled with the numbers in being_sorted
digits = [[] for i in range(10)]
# selecting the digit at the current iteration index and placing it in the correct bucket
for item in being_sorted: # N items
item_as_a_string = str(item)
try :
digit = int(item_as_a_string[index])
except IndexError:
digit = 0
digits[digit].append(item)
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))
Output:
[1, 40, 89, 163, 182, 183, 199, 355, 373, 524, 535, 559, 621, 641, 689, 830, 921, 959, 961]