mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-07 10:50:36 +00:00
add file from school
This commit is contained in:
29
partition_fusion/liste.py
Normal file
29
partition_fusion/liste.py
Normal file
@@ -0,0 +1,29 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Feb 10 09:15:57 2025
|
||||
|
||||
@author: elouan.fare
|
||||
"""
|
||||
|
||||
|
||||
|
||||
|
||||
def creer_liste():
|
||||
return ()
|
||||
|
||||
def est_vide(liste):
|
||||
return len(liste) == 0
|
||||
|
||||
def ajouter(liste,element):
|
||||
return (element,liste)
|
||||
|
||||
def tete(liste):
|
||||
assert not(est_vide(liste)), "Liste vide"
|
||||
element, reste = liste
|
||||
return element
|
||||
|
||||
def queue(liste):
|
||||
assert not(est_vide(liste)), "Liste vide"
|
||||
element,reste = liste
|
||||
return reste
|
||||
68
partition_fusion/partition_fusion.py
Normal file
68
partition_fusion/partition_fusion.py
Normal file
@@ -0,0 +1,68 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Feb 10 09:15:57 2025
|
||||
|
||||
@author: elouan.fare
|
||||
"""
|
||||
|
||||
|
||||
import liste as fifo
|
||||
|
||||
|
||||
def partition(liste, debut, fin):
|
||||
liste1 = liste
|
||||
liste2 = liste
|
||||
if fifo.est_vide(liste):
|
||||
return (fifo.creer_liste(), fifo.creer_liste())
|
||||
else:
|
||||
left_liste = fifo.creer_liste()
|
||||
for _ in range(int((debut + fin )/2)):
|
||||
liste1 = fifo.tete(liste1)
|
||||
for _ in range(debut,fin):
|
||||
left_liste = fifo.ajouter(left_liste, fifo.queue(liste2))
|
||||
return liste1, left_liste
|
||||
|
||||
|
||||
def fusion(tab, debut, milieu, fin):
|
||||
gauche = tab[debut:milieu + 1]
|
||||
droite = tab[milieu + 1:fin + 1]
|
||||
i, j, k = 0, 0, debut
|
||||
|
||||
while i < len(gauche) and j < len(droite):
|
||||
if gauche[i] <= droite[j]:
|
||||
tab[k] = gauche[i]
|
||||
i += 1
|
||||
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 = fifo.creer_liste()
|
||||
for i in range(10):
|
||||
liste = (liste, i)
|
||||
print(liste)
|
||||
|
||||
liste = fifo.queue(liste)
|
||||
print(liste)
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user