Skip to content

Instantly share code, notes, and snippets.

@rohit-jamuar
Created April 8, 2015 20:16
Show Gist options
  • Select an option

  • Save rohit-jamuar/7a0d921437eb2b0c36fc to your computer and use it in GitHub Desktop.

Select an option

Save rohit-jamuar/7a0d921437eb2b0c36fc to your computer and use it in GitHub Desktop.
O(kn) integer sorting
def max_num_len(arr):
x = max(arr)
count = 0
while x > 0:
x /= 10
count += 1
return count
def radix_sort(arr):
for i in range(max_num_len(arr) + 1):
bins = [[] for _ in range(10)]
for elem in arr:
bins[(elem / (10 ** i)) % 10].append(elem)
arr = []
for section in bins:
arr.extend(section)
return arr
if __name__ == '__main__':
from random import shuffle
z = range(25)
shuffle(z)
print z
print radix_sort(z)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment