Radix Sort In Python

Sunday, 17 June 2012
def radix_sort(array,k):
    """k is the max no. of digits in the number in array"""
    num=10
    max_num=10**k
    while num!=max_num:
        def key_arr(x):
            return x%num
        array.sort(key=key_arr)
        num=num*10
    return array

#############################################
array=list(map(int,input().split()))
print(array)
#for a maximum of 4 digit number
array=radix_sort(array,4)
print(array)
#############################################

Copyright @ 2013 code-craft. Designed by Templateism | MyBloggerLab