Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sort/radix_sort (#395) #397

Merged
merged 2 commits into from
Sep 4, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 6 additions & 8 deletions algorithms/sort/radix_sort.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
"""
radix sort
complexity: O(nk) . n is the size of input list and k is the digit length of the number
complexity: O(nk + n) . n is the size of input list and k is the digit length of the number
"""
def radix_sort(arr, simulation=False):
is_done = False
position = 1
max_number = max(arr)

iteration = 0
if simulation:
print("iteration",iteration,":",*arr)
print("iteration", iteration, ":", *arr)

while not is_done:
while position < max_number:
queue_list = [list() for _ in range(10)]
is_done = True

for num in arr:
digit_number = num // position % 10
queue_list[digit_number].append(num)
if is_done and digit_number > 0:
is_done = False

index = 0
for numbers in queue_list:
Expand All @@ -28,7 +25,8 @@ def radix_sort(arr, simulation=False):

if simulation:
iteration = iteration + 1
print("iteration",iteration,":",*arr)
print("iteration", iteration, ":", *arr)

position *= 10
return arr