from random import randrange
def is_sorted(l):
if len(l) == 0:
return True
dl = [ l[i+1] - l[i] for i in range(len(l)-1) ]
for e in dl:
if e < 0 :
return False
return True
def shuffle(l):
for i in range(len(l) - 1, 0, -1):
j = randrange(i + 1)
l[i],l[j] = l[j],l[i]
return l
def sort(l):
while is_sorted(l) == False:
l = shuffle(l)
return l
Est un code admissible
:
>>> sort([5,8,9,1,2])
[1, 2, 5, 8, 9]
Mais est-ce optimal 