Created
March 12, 2013 11:18
-
-
Save vierbergenlars/5142135 to your computer and use it in GitHub Desktop.
Fastest sorting algorithm ever! Introducing BogoSort
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
#!/usr/bin/env python | |
""" | |
http://acm.zhihua-lai.com | |
BogoSort.py | |
Bogo Sorting Algorithm | |
30/May/2012 | |
https://raw.github.com/DoctorLai/Algorithms/master/Sorting/BogoSort.py | |
""" | |
from random import * | |
from time import * | |
seed() | |
x = [] | |
for i in range(0, 10): | |
x.append(randint(0, 100)) | |
def inorder(x): | |
i = 0 | |
j = len(x) | |
while i + 1 < j: | |
if x[i] > x[i + 1]: | |
return False | |
i += 1 | |
return True | |
def bogo(x): | |
while not inorder(x): | |
shuffle(x) | |
return x | |
start = time() | |
print "Before: ", x | |
x = bogo(x) | |
print "After: ", x | |
print "%.2f seconds" % (time() - start) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment