Skip to content

Instantly share code, notes, and snippets.

@ejamesc
Created January 7, 2014 11:10
Show Gist options
  • Select an option

  • Save ejamesc/8297902 to your computer and use it in GitHub Desktop.

Select an option

Save ejamesc/8297902 to your computer and use it in GitHub Desktop.
Radixsort in Python, for a meetup in Ho Chi Minh City
def digit(num, curr_dig):
diff = pow(10, curr_dig - 1)
num = num / diff
return num % 10
def radix_sort(arr, n):
buckets = [[] for x in range(0, 10)]
curr_dig = 1
for i in range(0, n):
for i in range(0, len(arr)):
buckets[(digit(arr[i], curr_dig))].append(arr[i])
print buckets
arr = []
for i in range(0, 10):
arr = arr + buckets[i]
curr_dig = curr_dig + 1
buckets = [[] for x in range(0, 10)] # reset buckets
return arr
input = [1, 14, 700, 69, 1175, 22, 350, 55, 66, 245, 602, 3341]
print radix_sort(input, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment