Created
April 9, 2014 13:28
-
-
Save AndrewHanes/10270449 to your computer and use it in GitHub Desktop.
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
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