mirror of
https://hub.njuu.cf/TheAlgorithms/Python.git
synced 2023-10-11 13:06:12 +08:00
Fix sorts/radix_sort (#338)
This commit is contained in:
parent
faf16d7ced
commit
17a6d1c1a7
@ -1,28 +1,26 @@
|
|||||||
def radixsort(lst):
|
def radixsort(lst):
|
||||||
RADIX = 10
|
RADIX = 10
|
||||||
maxLength = False
|
placement = 1
|
||||||
tmp , placement = -1, 1
|
|
||||||
|
|
||||||
while not maxLength:
|
# get the maximum number
|
||||||
maxLength = True
|
max_digit = max(lst)
|
||||||
# declare and initialize buckets
|
|
||||||
buckets = [list() for _ in range( RADIX )]
|
|
||||||
|
|
||||||
# split lst between lists
|
while placement < max_digit:
|
||||||
for i in lst:
|
# declare and initialize buckets
|
||||||
tmp = int((i / placement) % RADIX)
|
buckets = [list() for _ in range( RADIX )]
|
||||||
buckets[tmp].append(i)
|
|
||||||
|
|
||||||
if maxLength and tmp > 0:
|
# split lst between lists
|
||||||
maxLength = False
|
for i in lst:
|
||||||
|
tmp = int((i / placement) % RADIX)
|
||||||
|
buckets[tmp].append(i)
|
||||||
|
|
||||||
# empty lists into lst array
|
# empty lists into lst array
|
||||||
a = 0
|
a = 0
|
||||||
for b in range( RADIX ):
|
for b in range( RADIX ):
|
||||||
buck = buckets[b]
|
buck = buckets[b]
|
||||||
for i in buck:
|
for i in buck:
|
||||||
lst[a] = i
|
lst[a] = i
|
||||||
a += 1
|
a += 1
|
||||||
|
|
||||||
# move to next
|
# move to next
|
||||||
placement *= RADIX
|
placement *= RADIX
|
||||||
|
Loading…
Reference in New Issue
Block a user