forked from TheAlgorithms/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add doctests to radix_sort() (TheAlgorithms#2148)
* 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>
- Loading branch information
Showing
3 changed files
with
33 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,26 +1,29 @@ | ||
def radix_sort(lst): | ||
RADIX = 10 | ||
placement = 1 | ||
from typing import List | ||
|
||
# get the maximum number | ||
max_digit = max(lst) | ||
|
||
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 buckets | ||
# declare and initialize empty buckets | ||
buckets = [list() for _ in range(RADIX)] | ||
|
||
# split lst between lists | ||
for i in lst: | ||
# split list_of_ints between the buckets | ||
for i in list_of_ints: | ||
tmp = int((i / placement) % RADIX) | ||
buckets[tmp].append(i) | ||
|
||
# empty lists into lst array | ||
# put each buckets' contents into list_of_ints | ||
a = 0 | ||
for b in range(RADIX): | ||
buck = buckets[b] | ||
for i in buck: | ||
lst[a] = i | ||
for i in buckets[b]: | ||
list_of_ints[a] = i | ||
a += 1 | ||
|
||
# move to next | ||
placement *= RADIX | ||
return list_of_ints |