Skip to content
Snippets Groups Projects
Verified Commit a675ca30 authored by orestis.malaspin's avatar orestis.malaspin
Browse files

added exercise

parent e940748f
No related branches found
No related tags found
No related merge requests found
Pipeline #15804 passed
...@@ -1169,7 +1169,7 @@ graph TD; ...@@ -1169,7 +1169,7 @@ graph TD;
* On fait les 2 dernières étapes en vitesse. * On fait les 2 dernières étapes en vitesse.
* Échange `2` avec `1`. * Échange `2` avec `1`.
* Il reste que `1`. * Il reste que `1`. GGWP!
:::: ::::
...@@ -1179,3 +1179,71 @@ graph TD; ...@@ -1179,3 +1179,71 @@ graph TD;
``` ```
| 1 | 2 | 4 | 5 | 6 | 8 | 10 | 12 | 16 | 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)
. . .
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment