Created
February 3, 2015 02:09
-
-
Save m00nlight/7f3d477e541e47d53ec1 to your computer and use it in GitHub Desktop.
Python compact radix sort of numbers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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