Skip to content

Instantly share code, notes, and snippets.

@vierbergenlars
Created March 12, 2013 11:18
Show Gist options
  • Save vierbergenlars/5142135 to your computer and use it in GitHub Desktop.
Save vierbergenlars/5142135 to your computer and use it in GitHub Desktop.
Fastest sorting algorithm ever! Introducing BogoSort
#!/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