Python/sorts/radix_sort.py

27 lines
613 B
Python
Raw Normal View History

def radix_sort(lst):
2019-02-09 02:14:23 +00:00
RADIX = 10
placement = 1
2017-09-29 21:47:24 +00:00
2019-02-09 02:14:23 +00:00
# get the maximum number
max_digit = max(lst)
2017-09-29 21:47:24 +00:00
2019-02-09 02:14:23 +00:00
while placement < max_digit:
2019-10-05 05:14:13 +00:00
# declare and initialize buckets
buckets = [list() for _ in range(RADIX)]
2017-10-23 13:47:45 +00:00
2019-10-05 05:14:13 +00:00
# split lst between lists
for i in lst:
tmp = int((i / placement) % RADIX)
buckets[tmp].append(i)
2017-09-29 21:47:24 +00:00
2019-10-05 05:14:13 +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
2019-10-05 05:14:13 +00:00
# move to next
placement *= RADIX