Created
February 4, 2012 20:46
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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) |
try(random_data = [0,001,0020,030,00005,006])
@vjeranc fixed that case:
https://gist.github.com/neonb88/75dd5d6dad35bc85f0d726e246822b63
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
try radix_sort([10000, 1000, 100, 10, 0])