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

added C code

parent a675ca30
Branches
No related tags found
No related merge requests found
Pipeline #15819 passed
...@@ -17,7 +17,7 @@ patat: ...@@ -17,7 +17,7 @@ patat:
# Un joli site # Un joli site
## VIsualisation d'algorithmes ## Visualisation d'algorithmes
* <https://visualgo.net/> * <https://visualgo.net/>
* Allons nous rafraîchir la mémoire sur l'insertion / recherche dans un arbre * Allons nous rafraîchir la mémoire sur l'insertion / recherche dans un arbre
...@@ -1193,7 +1193,7 @@ graph TD; ...@@ -1193,7 +1193,7 @@ graph TD;
* Postez le résultat sur matrix. * Postez le résultat sur matrix.
# L'algorithme du tri par tas (1/3) # L'algorithme du tri par tas (1/4)
## Deux étapes ## Deux étapes
...@@ -1221,7 +1221,7 @@ promotion(tab, i) ...@@ -1221,7 +1221,7 @@ promotion(tab, i)
promotion(tab, ind_max) promotion(tab, ind_max)
``` ```
# L'algorithme du tri par tas (2/3) # L'algorithme du tri par tas (2/4)
* Fonctions utilitaires * Fonctions utilitaires
...@@ -1240,10 +1240,65 @@ int droite(i) ...@@ -1240,10 +1240,65 @@ int droite(i)
``` ```
# L'algorithme du tri par tas (2/2) # L'algorithme du tri par tas (3/4)
\footnote size
## Implémenter en C l'algorithme du tri par tas (matrix, 20min) ## Implémenter en C l'algorithme du tri par tas (matrix, 20min)
. . . . . .
```C
void heapsort(int size, int tab[size]) {
heapify(size, tab);
swap(tab, tab + size - 1);
for (int s = size - 1; s > 1; s--) {
sift_up(s, tab, 0);
swap(tab, tab + s - 1);
}
}
void heapify(int size, int tab[size]) {
for (int i = size / 2 - 1; i >= 0; i--) {
sift_up(size, tab, i);
}
}
void sift_up(int size, int tab[size], int i) {
int ind_max = ind_max3(size, tab, i, left(i), right(i));
if (i != ind_max) {
swap(tab + i, tab + ind_max);
sift_up(size, tab, ind_max);
}
}
```
# L'algorithme du tri par tas (3/4)
\footnotesize
## Fonctions utilitaires
. . .
```C
int ind_max3(int size, int tab[size], int i, int l, int r) {
int ind_max = i;
if (l < size && tab[ind_max] < tab[l]) {
ind_max = l;
}
if (r < size && tab[ind_max] < tab[r]) {
ind_max = r;
}
return ind_max;
}
void swap(int *a, int *b) {
int tmp = *a;
*a = *b;
*b = tmp;
}
int left(int i) {
return 2 * i + 1;
}
int right(int i) {
return 2 * i + 2;
}
```
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment