Created
July 4, 2019 16:59
-
-
Save tuvo1106/b8930146b426a4e29a6d5bb7577ba026 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
def get_digit(num, i): | |
"""Return the (i)th index of num""" | |
return (num // 10**i) % 10 | |
def max_digits(arr): | |
"""Return the length of the longest element in array""" | |
return len(str(max(arr))) | |
def radix_sort(l:list): | |
"""Sort an array of positive integers""" | |
arr = l[::] | |
max_len = max_digits(arr) | |
for i in range(max_len): | |
res = [[] for x in range(10)] | |
for k in arr: | |
digit = get_digit(k, i) | |
res[digit].append(k) | |
arr = [y for x in res for y in x] | |
return arr |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment