MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / msd_radix_sort

Function msd_radix_sort

sorts/msd_radix_sort.py:11–37  ·  view source on GitHub ↗

Implementation of the MSD radix sort algorithm. Only works with positive integers :param list_of_ints: A list of integers :return: Returns the sorted list >>> msd_radix_sort([40, 12, 1, 100, 4]) [1, 4, 12, 40, 100] >>> msd_radix_sort([]) [] >>> msd_radix_sort([12

(list_of_ints: list[int])

Source from the content-addressed store, hash-verified

9
10
11def msd_radix_sort(list_of_ints: list[int]) -> list[int]:
12 """
13 Implementation of the MSD radix sort algorithm. Only works
14 with positive integers
15 :param list_of_ints: A list of integers
16 :return: Returns the sorted list
17 >>> msd_radix_sort([40, 12, 1, 100, 4])
18 [1, 4, 12, 40, 100]
19 >>> msd_radix_sort([])
20 []
21 >>> msd_radix_sort([123, 345, 123, 80])
22 [80, 123, 123, 345]
23 >>> msd_radix_sort([1209, 834598, 1, 540402, 45])
24 [1, 45, 1209, 540402, 834598]
25 >>> msd_radix_sort([-1, 34, 45])
26 Traceback (most recent call last):
27 ...
28 ValueError: All numbers must be positive
29 """
30 if not list_of_ints:
31 return []
32
33 if min(list_of_ints) < 0:
34 raise ValueError("All numbers must be positive")
35
36 most_bits = max(len(bin(x)[2:]) for x in list_of_ints)
37 return _msd_radix_sort(list_of_ints, most_bits)
38
39
40def _msd_radix_sort(list_of_ints: list[int], bit_position: int) -> list[int]:

Callers

nothing calls this directly

Calls 1

_msd_radix_sortFunction · 0.85

Tested by

no test coverage detected