2020-06-23 13:37:24 +00:00
|
|
|
from typing import List
|
2017-09-29 21:47:24 +00:00
|
|
|
|
|
|
|
|
2020-06-23 13:37:24 +00:00
|
|
|
def radix_sort(list_of_ints: List[int]) -> List[int]:
|
|
|
|
"""
|
|
|
|
radix_sort(range(15)) == sorted(range(15))
|
|
|
|
True
|
|
|
|
radix_sort(reversed(range(15))) == sorted(range(15))
|
|
|
|
True
|
|
|
|
"""
|
|
|
|
RADIX = 10
|
|
|
|
placement = 1
|
|
|
|
max_digit = max(list_of_ints)
|
2019-02-09 02:14:23 +00:00
|
|
|
while placement < max_digit:
|
2020-06-23 13:37:24 +00:00
|
|
|
# declare and initialize empty buckets
|
2019-10-05 05:14:13 +00:00
|
|
|
buckets = [list() for _ in range(RADIX)]
|
2020-06-23 13:37:24 +00:00
|
|
|
# split list_of_ints between the buckets
|
|
|
|
for i in list_of_ints:
|
2019-10-05 05:14:13 +00:00
|
|
|
tmp = int((i / placement) % RADIX)
|
|
|
|
buckets[tmp].append(i)
|
2020-06-23 13:37:24 +00:00
|
|
|
# put each buckets' contents into list_of_ints
|
2019-10-05 05:14:13 +00:00
|
|
|
a = 0
|
|
|
|
for b in range(RADIX):
|
2020-06-23 13:37:24 +00:00
|
|
|
for i in buckets[b]:
|
|
|
|
list_of_ints[a] = i
|
2019-10-05 05:14:13 +00:00
|
|
|
a += 1
|
|
|
|
# move to next
|
|
|
|
placement *= RADIX
|
2020-06-23 13:37:24 +00:00
|
|
|
return list_of_ints
|