Skip to content

Instantly share code, notes, and snippets.

@tuvo1106
Created July 4, 2019 16:59
Show Gist options
  • Save tuvo1106/b8930146b426a4e29a6d5bb7577ba026 to your computer and use it in GitHub Desktop.
Save tuvo1106/b8930146b426a4e29a6d5bb7577ba026 to your computer and use it in GitHub Desktop.
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