Python/sorts/radix_sort.py

49 lines
1.2 KiB
Python
Raw Normal View History

"""
This is a pure Python implementation of the radix sort algorithm
Source: https://en.wikipedia.org/wiki/Radix_sort
"""
from __future__ import annotations
2017-09-29 21:47:24 +00:00
RADIX = 10
2017-09-29 21:47:24 +00:00
def radix_sort(list_of_ints: list[int]) -> list[int]:
"""
Examples:
>>> radix_sort([0, 5, 3, 2, 2])
[0, 2, 2, 3, 5]
>>> radix_sort(list(range(15))) == sorted(range(15))
True
>>> radix_sort(list(range(14,-1,-1))) == sorted(range(15))
True
>>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000])
2020-08-31 16:55:56 +00:00
True
"""
placement = 1
max_digit = max(list_of_ints)
2020-08-31 16:55:56 +00:00
while placement <= max_digit:
# declare and initialize empty buckets
buckets: list[list] = [[] for _ in range(RADIX)]
# 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)
# put each buckets' contents into list_of_ints
2019-10-05 05:14:13 +00:00
a = 0
for b in range(RADIX):
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
return list_of_ints
if __name__ == "__main__":
import doctest
doctest.testmod()