Last active
July 17, 2016 17:26
-
-
Save nilskoppelmann/7ececee1c72608be348a0f5954daee2b to your computer and use it in GitHub Desktop.
Counting Sort Python (for positive numbers only)
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 counting_sort(a, mn, mx): | |
mx = max(a) if mx > max(a) else mx | |
count, result = [0] * (max(a) + 1), [] | |
i, j = 0, mn | |
while i < len(a): | |
count[a[i]] += 1 | |
i += 1 | |
while j <= mx: | |
result += [j] * count[j] | |
j += 1 | |
return result | |
a = [2, 9, 3, 8, 5, 43, 0, 1, 2] | |
print(counting_sort(a, 0, 22)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
suggesting a different data-type to represent numbers including negative range
dictionary
?