Python/sorts/radix_sort.py

29 lines
581 B
Python
Raw Normal View History

2017-02-25 15:06:58 +00:00
def radixsort(lst):
RADIX = 10
maxLength = False
tmp , placement = -1, 1
2017-09-29 21:47:24 +00:00
2017-02-25 15:06:58 +00:00
while not maxLength:
maxLength = True
# declare and initialize buckets
buckets = [list() for _ in range( RADIX )]
2017-09-29 21:47:24 +00:00
2017-02-25 15:06:58 +00:00
# split lst between lists
for i in lst:
tmp = int((i / placement) % RADIX)
buckets[tmp].append(i)
2017-10-23 13:47:45 +00:00
2017-02-25 15:06:58 +00:00
if maxLength and tmp > 0:
maxLength = False
2017-09-29 21:47:24 +00:00
2017-02-25 15:06:58 +00:00
# empty lists into lst array
a = 0
for b in range( RADIX ):
buck = buckets[b]
for i in buck:
lst[a] = i
a += 1
2017-09-29 21:47:24 +00:00
2017-02-25 15:06:58 +00:00
# move to next
placement *= RADIX