Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions sorts/radix_sort.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,27 @@

def radix_sort(list_of_ints: list[int]) -> list[int]:
"""
Examples:
Sort a list of non-negative integers using radix sort.

This implementation works only with non-negative values because it
iterates over the decimal digits of each number.

>>> 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))
>>> radix_sort(list(range(14, -1, -1))) == sorted(range(15))
True
>>> radix_sort([1,100,10,1000]) == sorted([1,100,10,1000])
>>> radix_sort([1, 100, 10, 1000]) == sorted([1, 100, 10, 1000])
True
>>> radix_sort([3, 1, -1, 2])
Traceback (most recent call last):
...
ValueError: All numbers in list_of_ints must be non-negative
"""
if any(item < 0 for item in list_of_ints):
raise ValueError("All numbers in list_of_ints must be non-negative")

placement = 1
max_digit = max(list_of_ints)
while placement <= max_digit:
Expand Down