Created
February 24, 2013 00:42
-
-
Save sahid/5022081 to your computer and use it in GitHub Desktop.
Bucket Sort in Python
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
def bsort(A): | |
"""Returns A sorted. with A = {x : x such that 0 <= x < 1}.""" | |
buckets = [[] for x in range(10)] | |
for i, x in enumerate(A): | |
buckets[int(x*len(buckets))].append(x) | |
out = [] | |
for buck in buckets: | |
out += isort(buck) | |
return out | |
def isort(A): | |
if len(A) <= 1: return A | |
i = 1 | |
while i < len(A): | |
k = A[i] | |
j = i - 1 | |
while j >= 0 and A[j] > k: | |
A[j+1] = A[j] | |
A[j] = k | |
j -= 1 | |
i += 1 | |
return A |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
did not understand why
"buckets[int(x*len(buckets))].append(x)"
sould be i | len(buckets)