Skip to content

Instantly share code, notes, and snippets.

@LeoAdamek
Created January 22, 2012 17:35
Show Gist options
  • Save LeoAdamek/1657781 to your computer and use it in GitHub Desktop.
Save LeoAdamek/1657781 to your computer and use it in GitHub Desktop.
Bogosort - The world's least efficient sorting algorithm ever. Best O(n), average O(n * n!), worst O(infinity)
from random import randint as r
from random import shuffle
from time import time
from datetime import timedelta
def bogosort(d):
sorted = checkOrder(d)
while not sorted:
shuffle(d)
sorted = checkOrder(d)
return d
def checkOrder(d):
for i in range( len(d)-1 ):
if d[i] > d[i+1]:
return False
return True
if __name__ == '__main__':
# Test Script with performance readout.
data = [r(0,500) for i in range(25)]
start_time = time()
bogo_data = bogosort(data)
print 'Sort of data took: ',timedelta( seconds = time() - start_time)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment