Sorting algorithms/Bogosort: Difference between revisions

Line 418:
last = x
return True</python>
 
Alternative definition for ''in_order'' (Python 2.5)
<python>def in_order(l):
return not l or all( l[i] < l[i+1] for i in range(0,len(l)-1))</python>
 
An alternative implementation for Python 2.5 or later:
 
<python>
import random
def bogosort(lst):
random.shuffle(lst) # must shuffle it first or it's a bug if lst was pre-sorted! :)
while lst != sorted(lst):
random.shuffle(lst)
return lst
</python>
Anonymous user