mirror of
https://github.com/Fare-spec/cours.git
synced 2025-12-08 03:00:37 +00:00
Add university content
This commit is contained in:
47
high-school/partition_fusion/code.py
Normal file
47
high-school/partition_fusion/code.py
Normal file
@@ -0,0 +1,47 @@
|
||||
def partition(tab, debut, fin):
|
||||
pivot = tab[fin]
|
||||
i = debut - 1
|
||||
for j in range(debut, fin):
|
||||
if tab[j] <= pivot:
|
||||
i += 1
|
||||
tab[i], tab[j] = tab[j], tab[i]
|
||||
tab[i + 1], tab[fin] = tab[fin], tab[i + 1]
|
||||
return i + 1
|
||||
|
||||
|
||||
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)
|
||||
|
||||
|
||||
tableau = [34, 7, 23, 32, 5, 62, 32, 8, 9]
|
||||
tri_fusion_partition(tableau, 0, len(tableau) - 1)
|
||||
print("Tableau trié :", tableau)
|
||||
31
high-school/partition_fusion/code2.py
Normal file
31
high-school/partition_fusion/code2.py
Normal file
@@ -0,0 +1,31 @@
|
||||
from random import shuffle
|
||||
|
||||
i = 0
|
||||
|
||||
|
||||
def fusion(liste_1, liste_2):
|
||||
global i
|
||||
if len(liste_1) == 0:
|
||||
return liste_2
|
||||
if len(liste_2) == 0:
|
||||
return liste_1
|
||||
if liste_1[0] <= liste_2[0]:
|
||||
i += 1
|
||||
return [liste_1[0]] + fusion(liste_1[1:], liste_2)
|
||||
i += 1
|
||||
return [liste_2[0]] + fusion(liste_1, liste_2[1:])
|
||||
|
||||
|
||||
def tri_pf(liste):
|
||||
global i
|
||||
if len(liste) <= 1:
|
||||
return liste
|
||||
i += 1
|
||||
millieu = len(liste) // 2
|
||||
return fusion(tri_pf(liste[:millieu]), tri_pf(liste[millieu:]))
|
||||
|
||||
|
||||
test = [23, 8, 20, 10, 13, 1]
|
||||
print(test, i)
|
||||
test = tri_pf(test)
|
||||
print(test, i)
|
||||
31
high-school/partition_fusion/liste.py
Normal file
31
high-school/partition_fusion/liste.py
Normal file
@@ -0,0 +1,31 @@
|
||||
#!/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, _ = liste
|
||||
return element
|
||||
|
||||
|
||||
def queue(liste):
|
||||
assert not (est_vide(liste)), "Liste vide"
|
||||
_, reste = liste
|
||||
return reste
|
||||
91
high-school/partition_fusion/partition_fusion.py
Normal file
91
high-school/partition_fusion/partition_fusion.py
Normal file
@@ -0,0 +1,91 @@
|
||||
#!/usr/bin/env python3
|
||||
# -*- coding: utf-8 -*-
|
||||
"""
|
||||
Created on Mon Feb 10 09:15:57 2025
|
||||
|
||||
@author: elouan.fare
|
||||
"""
|
||||
|
||||
|
||||
import liste as fifo
|
||||
|
||||
|
||||
def taille(liste):
|
||||
a = 0
|
||||
while not (fifo.est_vide(liste)):
|
||||
liste = fifo.queue(liste)
|
||||
a += 1
|
||||
return a
|
||||
|
||||
|
||||
def divise2(liste):
|
||||
n = taille(liste)
|
||||
droite, gauche = partition(liste, 0, n // 2)
|
||||
return droite, gauche
|
||||
|
||||
|
||||
def renverser(liste):
|
||||
result = fifo.creer_liste()
|
||||
while not fifo.est_vide(liste):
|
||||
result = fifo.ajouter(result, fifo.tete(liste))
|
||||
liste = fifo.queue(liste)
|
||||
return result
|
||||
|
||||
|
||||
def partition(liste, debut, fin):
|
||||
if fifo.est_vide(liste):
|
||||
return fifo.creer_liste(), fifo.creer_liste()
|
||||
|
||||
current = liste
|
||||
for _ in range(debut):
|
||||
if fifo.est_vide(current):
|
||||
break
|
||||
current = fifo.queue(current)
|
||||
|
||||
segment_inversé = fifo.creer_liste()
|
||||
for _ in range(fin - debut):
|
||||
if fifo.est_vide(current):
|
||||
break
|
||||
segment_inversé = fifo.ajouter(segment_inversé, fifo.tete(current))
|
||||
current = fifo.queue(current)
|
||||
|
||||
segment = renverser(segment_inversé)
|
||||
|
||||
return current, segment
|
||||
|
||||
|
||||
def fusion(gauche, droite):
|
||||
if fifo.est_vide(gauche):
|
||||
return droite
|
||||
if fifo.est_vide(droite):
|
||||
return gauche
|
||||
|
||||
if fifo.tete(gauche) <= fifo.tete(droite):
|
||||
return fifo.ajouter(fusion(fifo.queue(gauche), droite), fifo.tete(gauche))
|
||||
else:
|
||||
return fifo.ajouter(fusion(gauche, fifo.queue(droite)), fifo.tete(droite))
|
||||
|
||||
|
||||
def merge_sort(liste):
|
||||
|
||||
if fifo.est_vide(liste):
|
||||
return liste
|
||||
elif fifo.est_vide(fifo.queue(liste)):
|
||||
return liste # si elle ne contient que un elt
|
||||
gauche, droite = divise2(liste)
|
||||
|
||||
tri1 = merge_sort(gauche) # recursif
|
||||
tri2 = merge_sort(droite) # recursif
|
||||
|
||||
return fusion(tri1, tri2)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
liste = fifo.creer_liste()
|
||||
for i in reversed(range(100)):
|
||||
liste = fifo.ajouter(liste, i)
|
||||
print(liste)
|
||||
|
||||
print("Après avoir trié la liste:")
|
||||
|
||||
print(merge_sort(liste))
|
||||
Reference in New Issue
Block a user