Skip to content

Instantly share code, notes, and snippets.

@m00nlight
Created February 3, 2015 02:09
Show Gist options
  • Save m00nlight/7f3d477e541e47d53ec1 to your computer and use it in GitHub Desktop.
Save m00nlight/7f3d477e541e47d53ec1 to your computer and use it in GitHub Desktop.
Python compact radix sort of numbers
class RadixSort:
def radix_sort(self, nums):
"""
Input an number list and return an sortd list
Type: [Int] -> [Int]
"""
max_len = max(map(lambda x: len(str(x)), nums))
tmp, base = 1, 10
for i in range(max_len):
buckets = [[] for i in range(10)]
for num in nums:
buckets[num / tmp % base].append(num)
nums = [x for bucket in buckets for x in bucket]
tmp = tmp * 10
return nums
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment