def quicksort(tab, min, max):
def dessin():
return(
"min = %i, max=%i, pivot=%i, i=%i, j=%i"%(
min, max, pivot, i, j),
sum(point((pos,v), size=40,
color =
"blue" if pos < min or pos > max
else
"red" if v > pivot
else
"green")
for pos,v in enumerate(tab))
+ vert(i, tab, color="black")
+ vert(min, tab, color="blue")
+ vert(max, tab, color="blue")
+ horiz(pivot, tab))
if min < max:
pivot = tab[max]
i = min
j = max-1
yield dessin()
while True:
while tab[i] < pivot: i +=1
while tab[j] > pivot: j -=1
if i < j:
tab[i], tab[j] = tab[j], tab[i]
i += 1; j -= 1
else:
tab[i], tab[max] = tab[max], tab[i]
break
yield dessin()
for step in quicksort(tab, min, i-1):
yield step
for step in quicksort(tab, i+1, max):
yield step