Skip to content

Instantly share code, notes, and snippets.

@hiroshi-manabe
Created November 26, 2011 08:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save hiroshi-manabe/1395308 to your computer and use it in GitHub Desktop.
Save hiroshi-manabe/1395308 to your computer and use it in GitHub Desktop.
Radix sort
base = 10
def radix_sort(x, max):
radix = 1
while radix < max:
x = counting_sort(x, radix)
radix *= base
return x
def counting_sort(a, radix):
c = [0] * base
for k in a:
r = (k / radix) % base
c[r] += 1
for i in range(1, base):
c[i] += c[i - 1]
b = [0] * len(a)
for k in reversed(a):
r = (k / radix) % base
b[c[r] - 1] = k
c[r] -= 1
return b
print radix_sort([925, 663, 212, 58, 775, 802], 1000)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment