-
Notifications
You must be signed in to change notification settings - Fork 0
/
radix_sort.py
83 lines (67 loc) · 2.38 KB
/
radix_sort.py
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
"""This module contains my implementations of radix sort."""
def radix_sort2(unsorted_list, base=10):
"""Implementation of radix sort. Assumes only positive integer inputs."""
if not unsorted_list:
return unsorted_list
power = 0
max_digits = find_max_digits(unsorted_list)
for digit in range(max_digits):
unsorted_list = counting_sort2(unsorted_list, base, power)
power += 1
return unsorted_list
def find_max_digits(unsorted_list):
"""Find the number of digits in the longest number in
the list.
"""
max_number = max(unsorted_list)
if max_number == 0:
return 1
num_digits = 0
while max_number > 0:
num_digits += 1
max_number //= 10
return num_digits
def counting_sort2(unsorted_list, base, power):
"""Implementation of counting sort to be used in radix sort."""
#build counts list
counts = [0] * base
#fill counts list
for number in unsorted_list:
digit = number // base ** power % base
counts[digit] += 1
#overwrite counts list
#value at each index represents the number of values <= that index
#in th original list
for index in range(1, len(counts)):
counts[index] = counts[index] + counts[index - 1]
#fill sorted list
sorted_list = [None] * len(unsorted_list)
for number in unsorted_list:
digit = number // base ** power % base
index = counts[digit] - 1
sorted_list[index] = number
counts[digit] -= 1
return sorted_list
#Another way to write radix sort using a different version of counting sort
def radix_sort(unsorted_list, base=10):
"""Implementation of radix sort."""
power = 0
#find the number of digits in the longest number
max_digits = find_max_digits(unsorted_list)
for digit in range(max_digits):
counting_sort(unsorted_list, base, power)
power += 1
def counting_sort(unsorted_list, base, power):
"""Implementation of counting sort to be used in radix sort."""
#create buckets list
buckets = [[] for num in range(base)]
#fill buckets
for number in unsorted_list:
digit = number // base ** power % base
buckets[digit].append(number)
#overwrite values in unsorted_list
index = 0
for bucket in buckets:
for number in bucket:
unsorted_list[index] = number
index += 1