mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
a4726ca248
* [mypy] Add/fix type annotations for recursive_insertion_sort(#4085) * [mypy] Add/fix type annotations for bucket_sort(#4085) * [mypy] Reworked code for cocktail_shaker_sort so that missing return statement error is resolved(#4085) * [mypy] Add/fix type annotations for patience_sort(#4085) * [mypy] Add/fix type annotations for radix_sort(#4085) Co-authored-by: goodm2 <4qjpngu8mem8cz>
53 lines
1.3 KiB
Python
53 lines
1.3 KiB
Python
"""
|
|
This is a pure Python implementation of the quick sort algorithm
|
|
For doctests run following command:
|
|
python -m doctest -v radix_sort.py
|
|
or
|
|
python3 -m doctest -v radix_sort.py
|
|
For manual testing run:
|
|
python radix_sort.py
|
|
"""
|
|
from __future__ import annotations
|
|
|
|
from typing import List
|
|
|
|
|
|
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])
|
|
True
|
|
"""
|
|
RADIX = 10
|
|
placement = 1
|
|
max_digit = max(list_of_ints)
|
|
while placement <= max_digit:
|
|
# declare and initialize empty buckets
|
|
buckets: List[list] = [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
|
|
|
|
|
|
if __name__ == "__main__":
|
|
import doctest
|
|
|
|
doctest.testmod()
|