Created
January 7, 2014 11:10
-
-
Save ejamesc/8297902 to your computer and use it in GitHub Desktop.
Radixsort in Python, for a meetup in Ho Chi Minh City
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 digit(num, curr_dig): | |
| diff = pow(10, curr_dig - 1) | |
| num = num / diff | |
| return num % 10 | |
| def radix_sort(arr, n): | |
| buckets = [[] for x in range(0, 10)] | |
| curr_dig = 1 | |
| for i in range(0, n): | |
| for i in range(0, len(arr)): | |
| buckets[(digit(arr[i], curr_dig))].append(arr[i]) | |
| print buckets | |
| arr = [] | |
| for i in range(0, 10): | |
| arr = arr + buckets[i] | |
| curr_dig = curr_dig + 1 | |
| buckets = [[] for x in range(0, 10)] # reset buckets | |
| return arr | |
| input = [1, 14, 700, 69, 1175, 22, 350, 55, 66, 245, 602, 3341] | |
| print radix_sort(input, 4) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment