Skip to content

Instantly share code, notes, and snippets.

@rizkyabdilah
Created February 4, 2012 20:46
Show Gist options
  • Save rizkyabdilah/1740053 to your computer and use it in GitHub Desktop.
Save rizkyabdilah/1740053 to your computer and use it in GitHub Desktop.
Implementation of radix sort algorithm in python http://en.wikipedia.org/wiki/Radix_sort
#!/usr/bin/env python
def radix_sort(random_list):
len_random_list = len(random_list)
modulus = 10
div = 1
while True:
# empty array, [[] for i in range(10)]
new_list = [[], [], [], [], [], [], [], [], [], []]
for value in random_list:
least_digit = value % modulus
least_digit /= div
new_list[least_digit].append(value)
modulus = modulus * 10
div = div * 10
if len(new_list[0]) == len_random_list:
return new_list[0]
random_list = []
rd_list_append = random_list.append
for x in new_list:
for y in x:
rd_list_append(y)
random_data = [13, 8, 1992, 31, 3, 1993]
print radix_sort(random_data)
@vjeranc
Copy link

vjeranc commented Jul 26, 2014

try radix_sort([10000, 1000, 100, 10, 0])

@puneetraipuria
Copy link

try(random_data = [0,001,0020,030,00005,006])

@neonb88
Copy link

neonb88 commented Apr 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment