mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-08 03:00:37 +00:00
Add fusion fonction
This commit is contained in:
@@ -40,44 +40,21 @@ def partition(liste, debut, fin):
|
|||||||
return current, segment
|
return current, segment
|
||||||
|
|
||||||
|
|
||||||
def fusion(tab, debut, milieu, fin):
|
def fusion(gauche,droite):
|
||||||
gauche = tab[debut:milieu + 1]
|
if fifo.est_vide(gauche):
|
||||||
droite = tab[milieu + 1:fin + 1]
|
return droite
|
||||||
i, j, k = 0, 0, debut
|
if fifo.est_vide(droite):
|
||||||
|
return gauche
|
||||||
|
|
||||||
while i < len(gauche) and j < len(droite):
|
if fifo.tete(gauche)<=fifo.tete(droite):
|
||||||
if gauche[i] <= droite[j]:
|
return fifo.ajouter(fusion(fifo.queue(gauche),droite),fifo.tete(gauche))
|
||||||
tab[k] = gauche[i]
|
else:
|
||||||
i += 1
|
return fifo.ajouter(fusion(gauche,fifo.queue(droite)),fifo.tete(droite))
|
||||||
else:
|
|
||||||
tab[k] = droite[j]
|
|
||||||
j += 1
|
|
||||||
k += 1
|
|
||||||
|
|
||||||
while i < len(gauche):
|
|
||||||
tab[k] = gauche[i]
|
|
||||||
i += 1
|
|
||||||
k += 1
|
|
||||||
|
|
||||||
while j < len(droite):
|
|
||||||
tab[k] = droite[j]
|
|
||||||
j += 1
|
|
||||||
k += 1
|
|
||||||
|
|
||||||
def tri_fusion_partition(tab, debut, fin):
|
|
||||||
if debut < fin:
|
|
||||||
pivot_index = partition(tab, debut, fin)
|
|
||||||
tri_fusion_partition(tab, debut, pivot_index - 1)
|
|
||||||
tri_fusion_partition(tab, pivot_index + 1, fin)
|
|
||||||
fusion(tab, debut, pivot_index, fin)
|
|
||||||
|
|
||||||
|
|
||||||
liste_test = fifo.creer_liste()
|
liste_initiale = fifo.creer_liste()
|
||||||
for i in range(1,10):
|
for i in reversed(range(10)):
|
||||||
liste_test = fifo.ajouter(liste_test,i)
|
liste_initiale = fifo.ajouter(liste_initiale, i)
|
||||||
print(liste_test)
|
gauche, droite = partition(liste_initiale,3 ,7)
|
||||||
|
print(gauche, droite)
|
||||||
|
|
||||||
test = partition(liste_test,1,9)
|
|
||||||
|
|
||||||
|
|
||||||
print(test)
|
|
||||||
|
|||||||
Reference in New Issue
Block a user