From 17a6d1c1a7835cc3bf6adec7482d96a7ba391e9a Mon Sep 17 00:00:00 2001 From: Inno Fang Date: Sat, 9 Feb 2019 10:14:23 +0800 Subject: [PATCH] Fix sorts/radix_sort (#338) --- sorts/radix_sort.py | 42 ++++++++++++++++++++---------------------- 1 file changed, 20 insertions(+), 22 deletions(-) diff --git a/sorts/radix_sort.py b/sorts/radix_sort.py index b0b4b41ab..e4cee61f3 100644 --- a/sorts/radix_sort.py +++ b/sorts/radix_sort.py @@ -1,28 +1,26 @@ def radixsort(lst): - RADIX = 10 - maxLength = False - tmp , placement = -1, 1 + RADIX = 10 + placement = 1 - while not maxLength: - maxLength = True - # declare and initialize buckets - buckets = [list() for _ in range( RADIX )] + # get the maximum number + max_digit = max(lst) - # split lst between lists - for i in lst: - tmp = int((i / placement) % RADIX) - buckets[tmp].append(i) + while placement < max_digit: + # declare and initialize buckets + buckets = [list() for _ in range( RADIX )] - if maxLength and tmp > 0: - maxLength = False + # split lst between lists + for i in lst: + tmp = int((i / placement) % RADIX) + buckets[tmp].append(i) - # empty lists into lst array - a = 0 - for b in range( RADIX ): - buck = buckets[b] - for i in buck: - lst[a] = i - a += 1 + # empty lists into lst array + a = 0 + for b in range( RADIX ): + buck = buckets[b] + for i in buck: + lst[a] = i + a += 1 - # move to next - placement *= RADIX + # move to next + placement *= RADIX