Created
January 22, 2012 17:35
-
-
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)
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
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