diff --git a/slides/cours_16.md b/slides/cours_16.md index c0691fd655158bae18cad088b9c1d1c9b79d56c1..b805e9b3b6cc1b332acf849f272a407b1c125999 100644 --- a/slides/cours_16.md +++ b/slides/cours_16.md @@ -1169,7 +1169,7 @@ graph TD; * On fait les 2 dernières étapes en vitesse. * Échange `2` avec `1`. -* Il reste que `1`. +* Il reste que `1`. GGWP! :::: @@ -1179,3 +1179,71 @@ graph TD; ``` | 1 | 2 | 4 | 5 | 6 | 8 | 10 | 12 | 16 ``` + +# Exercice (10min) + +* Trier par tas le tableau + +``` + | 1 | 2 | 4 | 5 | 6 | 8 | 10 | 12 | 16 +``` + +* Mettez autant de détails que possible. +* Que constatez-vous? +* Postez le résultat sur matrix. + + +# L'algorithme du tri par tas (1/3) + +## Deux étapes + +1. Entassement (tamisage): transformer l'arbre en tas. +2. Échanger la racine avec le dernier élément et entasser la racine. + +## Pseudo-code d'entassement de l'arbre (5 min, matrix) + +. . . + +``` +tri_par_tas(tab) + entassement(tab) + échanger(tab[0], tab[size(tab)-1]) + pour i = size(tab)-1 à 2 + promotion(tab, i) + échanger(tab[0], tab[i-1]) +entassement(tab) + pour i = size(tab) / 2 - 1 jusqu'à 0 + promotion(tab, i) +promotion(tab, i) + ind_max = ind_max(tab, i, gauche(i), droite(i)) + si i != ind_max + échanger(tab[i], tab[ind_max]) + promotion(tab, ind_max) +``` + +# L'algorithme du tri par tas (2/3) + +* Fonctions utilitaires + +``` +int ind_max(tab, i, g, d) + ind_max = i + si tab[ind_max] < tab[l] + ind_max = l + si tab[ind_mx] < tab[r] + ind_max = r + retourne ind_max +int gauche(i) + retourne 2 * i + 1 +int droite(i) + retourne 2 * i + 2 +``` + + +# L'algorithme du tri par tas (2/2) + +## Implémenter en C l'algorithme du tri par tas (matrix, 20min) + +. . . + +