from random import randint
def affiche(liste, gauche, droit) :
print(' ', end=' ')
for x in liste :
print(x, end=' ')
print(' ', end=' ')
print()
for i in range(-1,len(liste)+1) :
if i == gauche : print('g', end=' ')
elif i == droit : print('d', end=' ')
else : print(' ', end=' ')
print()
if gauche == droit:
for i in range(-1,len(liste)+1) :
if i == droit : print('d', end=' ')
else : print(' ', end=' ')
print()
print("*****************************************************")
print()
def partitionDessusDessous_avecAffichage(liste) :
pivot = liste[0]
gauche = 1
droit = len(liste)-1
affiche(liste, gauche, droit)
while gauche <= droit :
while gauche <= droit and liste[gauche] <= pivot :
gauche +=1
affiche(liste, gauche, droit)
while gauche <= droit and liste[droit] > pivot :
droit -= 1
affiche(liste, gauche, droit)
if gauche < droit :
liste[gauche], liste[droit] = liste[droit], liste[gauche]
gauche += 1
droit -= 1
affiche(liste, gauche, droit)
liste[0], liste[gauche-1] = liste[gauche-1], liste[0]
L = [ randint(1,9) for _ in range(randint(5,12)) ]
print("Avant partition : ", L)
partitionDessusDessous_avecAffichage(L)
print("Après partition : ", L)
Quelques exemples de déroulement obtenus :
Avant partition : [6, 8, 1, 4, 5]
6 8 1 4 5
g d
*****************************************************
6 5 1 4 8
g d
*****************************************************
6 5 1 4 8
g
d
*****************************************************
6 5 1 4 8
d g
*****************************************************
Après partition : [4, 5, 1, 6, 8]
Avant partition : [1, 3, 1, 4, 4, 4, 5, 6, 7, 9, 4]
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 3 1 4 4 4 5 6 7 9 4
g d
*****************************************************
1 1 3 4 4 4 5 6 7 9 4
d g
*****************************************************
Après partition : [1, 1, 3, 4, 4, 4, 5, 6, 7, 9, 4]
Avant partition : [3, 6, 9, 8, 2, 3, 4, 5, 2, 5, 6]
3 6 9 8 2 3 4 5 2 5 6
g d
*****************************************************
3 6 9 8 2 3 4 5 2 5 6
g d
*****************************************************
3 6 9 8 2 3 4 5 2 5 6
g d
*****************************************************
3 2 9 8 2 3 4 5 6 5 6
g d
*****************************************************
3 2 9 8 2 3 4 5 6 5 6
g d
*****************************************************
3 2 9 8 2 3 4 5 6 5 6
g d
*****************************************************
3 2 3 8 2 9 4 5 6 5 6
g d
*****************************************************
3 2 3 2 8 9 4 5 6 5 6
d g
*****************************************************
Après partition : [2, 2, 3, 3, 8, 9, 4, 5, 6, 5, 6]
Avant partition : [9, 1, 5, 3, 3, 3, 3, 4, 5, 3, 1]
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
g
d
*****************************************************
9 1 5 3 3 3 3 4 5 3 1
d g
*****************************************************
Après partition : [1, 1, 5, 3, 3, 3, 3, 4, 5, 3, 9]
Avant partition : [2, 4, 4, 9, 2, 3]
2 4 4 9 2 3
g d
*****************************************************
2 4 4 9 2 3
g d
*****************************************************
2 2 4 9 4 3
g d
*****************************************************
2 2 4 9 4 3
g
d
*****************************************************
2 2 4 9 4 3
d g
*****************************************************
Après partition : [2, 2, 4, 9, 4, 3]