Skip to content

Instantly share code, notes, and snippets.

@AndrewHanes
Created April 9, 2014 13:28
Show Gist options
  • Save AndrewHanes/10270449 to your computer and use it in GitHub Desktop.
Save AndrewHanes/10270449 to your computer and use it in GitHub Desktop.
import sys
import time
from random import shuffle
from random import randint
def setup(n):
lst = []
for i in range(n):
lst.append(i)
shuffle(lst)
return lst
def isSorted(lst):
for i in range(len(lst)-1):
if lst[i] > lst[i+1]:
return False
return True
def bogo(lst,cmpFunc = isSorted):
while(not cmpFunc(lst)):
a = randint(0, len(lst) - 1)
b = randint(0, len(lst) - 1)
lst[a], lst[b] = lst[b], lst[a]
return lst
def main():
for i in range(20):
start = time.time()
lst = setup(i)
lst = bogo(lst)
print lst
print "Took: " + str(time.time() - start)
totalTime = time.time()
main()
print "Done, took " + str(time.time() - totalTime)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment