Last active
August 29, 2015 14:03
-
-
Save lgaff/78f1387e2ea60ec37f57 to your computer and use it in GitHub Desktop.
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 bogosort(source): | |
"""Sort by shuffling the input until it's in order. relies on probability allowing that (assuming a healthy shuffling algorithm), | |
eventually the list will appear in order""" | |
def in_order(source, iter): | |
sorted = True | |
i = 0 | |
print("(%d) in_order[%d]: %d < %d: %s :: %d <= %d: %s" %\ | |
(iter, i, i, len(source), i<len(source), source[i], source[i+1], source[i] <= source[i+1])) | |
while i < len(source)-1 and source[i] <= source[i+1]: | |
i += 1 | |
return i == len(source)-1 | |
out = list(source) | |
iterations = 0 | |
while not in_order(out, iterations): | |
iterations += 1 | |
out = shuffle(out) | |
return {'input' : source, 'output' : out, 'iterations' : iterations} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment