Skip to content

Instantly share code, notes, and snippets.

@lgaff
Last active August 29, 2015 14:03
Show Gist options
  • Save lgaff/78f1387e2ea60ec37f57 to your computer and use it in GitHub Desktop.
Save lgaff/78f1387e2ea60ec37f57 to your computer and use it in GitHub Desktop.
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