mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-27 23:11:09 +00:00
5b6ebf8f12
* Add doctests to radix_sort() * fixup! Format Python code with psf/black push * Update radix_sort.py * updating DIRECTORY.md Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
30 lines
842 B
Python
30 lines
842 B
Python
from typing import List
|
|
|
|
|
|
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)
|
|
while placement < max_digit:
|
|
# declare and initialize empty buckets
|
|
buckets = [list() for _ in range(RADIX)]
|
|
# split list_of_ints between the buckets
|
|
for i in list_of_ints:
|
|
tmp = int((i / placement) % RADIX)
|
|
buckets[tmp].append(i)
|
|
# put each buckets' contents into list_of_ints
|
|
a = 0
|
|
for b in range(RADIX):
|
|
for i in buckets[b]:
|
|
list_of_ints[a] = i
|
|
a += 1
|
|
# move to next
|
|
placement *= RADIX
|
|
return list_of_ints
|