Created
April 8, 2015 20:16
-
-
Save rohit-jamuar/7a0d921437eb2b0c36fc to your computer and use it in GitHub Desktop.
O(kn) integer sorting
This file contains hidden or 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
| def max_num_len(arr): | |
| x = max(arr) | |
| count = 0 | |
| while x > 0: | |
| x /= 10 | |
| count += 1 | |
| return count | |
| def radix_sort(arr): | |
| for i in range(max_num_len(arr) + 1): | |
| bins = [[] for _ in range(10)] | |
| for elem in arr: | |
| bins[(elem / (10 ** i)) % 10].append(elem) | |
| arr = [] | |
| for section in bins: | |
| arr.extend(section) | |
| return arr | |
| if __name__ == '__main__': | |
| from random import shuffle | |
| z = range(25) | |
| shuffle(z) | |
| print z | |
| print radix_sort(z) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment