Skip to content

Instantly share code, notes, and snippets.

@boopathi
Forked from hiroshi-manabe/radix_sort.py
Created November 26, 2011 09:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save boopathi/1395353 to your computer and use it in GitHub Desktop.
Save boopathi/1395353 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