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

updated pseudocodes to rfench

parent 1f67f0fa
Branches
No related tags found
No related merge requests found
...@@ -22,11 +22,11 @@ double tab[N]; ...@@ -22,11 +22,11 @@ double tab[N];
. . . . . .
```C ```C
double moyenne = 0.0; double mean = 0.0;
for (int i = 0; i < N; ++i) { // N assignations for (int i = 0; i < N; ++i) { // N assignations
moyenne += tab[i]; // N additions mean += tab[i]; // N additions
} }
moyenne /= N; // O(N) mean /= N; // O(N)
``` ```
. . . . . .
...@@ -36,12 +36,12 @@ moyenne /= N; // O(N) ...@@ -36,12 +36,12 @@ moyenne /= N; // O(N)
. . . . . .
```C ```C
double moyenne = moyenne(N, tab); // O(N) double mean = moyenne(N, tab); // O(N)
double ecart = 0.0; double dev = 0.0;
for (int i = 0; i < N; ++i) { for (int i = 0; i < N; ++i) {
ecart += pow(tab[i] - moyenne, 2); // N tours dev += pow(tab[i] - moyenne, 2); // N tours
} }
ecart = sqrt(ecart); ecart /= N; // O(2*N) = O(N) dev = sqrt(dev); dev /= N; // O(2*N) = O(N)
``` ```
# Tri par insertion (1/3) # Tri par insertion (1/3)
...@@ -59,7 +59,7 @@ triés du tableau. ...@@ -59,7 +59,7 @@ triés du tableau.
# Tri par insertion (2/3) # Tri par insertion (2/3)
## Exercice: Proposer un algorithme ## Exercice: Proposer un algorithme (en C)
. . . . . .
...@@ -167,16 +167,14 @@ int low, high; // les indices min/max des tableaux à trier ...@@ -167,16 +167,14 @@ int low, high; // les indices min/max des tableaux à trier
## Pseudocode: quicksort ## Pseudocode: quicksort
```C ```python
void quicksort(array, low, high) { rien quicksort(entier tableau[], ind_min, ind_max)
if (more than 1 elems) { si (longueur(tab) > 1)
pivot_ind = partition(array, low, high); ind_pivot = partition(array, ind_min, ind_max)
if (something left of pivot) si (longueur(tableau[0:ind_pivot-1]) != 0)
quicksort(array, low, pivot_ind - 1); quicksort(tableau, ind_min, pivot_ind - 1);
if (something right of pivot) si (longueur(tableau[ind_pivot+1:ind_max-1]) != 0)
quicksort(array, pivot_ind + 1, high); quicksort(tableau, ind_pivot + 1, ind_max)
}
}
``` ```
# Tri rapide ou quicksort (5/8) # Tri rapide ou quicksort (5/8)
...@@ -184,20 +182,20 @@ void quicksort(array, low, high) { ...@@ -184,20 +182,20 @@ void quicksort(array, low, high) {
## Pseudocode: partition ## Pseudocode: partition
```C ```C
int partition(array, low, high) { entier partition(entier tableau[], entier ind_min, entier ind_max)
pivot = array[high]; // choix arbitraire pivot = array[ind_max]; // choix arbitraire
i = low; i = ind_min;
j = high-1; j = ind_max-1;
while i < j { tant que i < j:
en remontant i trouver le premier élément > pivot; en remontant i trouver le premier élément > pivot;
en descendant j trouver le premier élément < pivot; en descendant j trouver le premier élément < pivot;
swap(array[i], array[j]); échanger(array[i], array[j]);
// les plus grands à droite // les plus grands à droite
// mettre les plus petits à gauche // mettre les plus petits à gauche
}
// on met le pivot "au milieu" // on met le pivot "au milieu"
swap(array[i], array[pivot]); échanger(array[i], array[pivot]);
return i; // on retourne l'indice pivot retourne i; // on retourne l'indice pivot
} }
``` ```
...@@ -259,10 +257,10 @@ int partition(int size, int array[size], int first, int last) { ...@@ -259,10 +257,10 @@ int partition(int size, int array[size], int first, int last) {
int i = first - 1, j = last; int i = first - 1, j = last;
do { do {
do { do {
i++; i += 1;
} while (array[i] < pivot && i < j); } while (array[i] < pivot && i < j);
do { do {
j--; j -= 1;
} while (array[j] > pivot && i < j); } while (array[j] > pivot && i < j);
if (j > i) { if (j > i) {
swap(&array[i], &array[j]); swap(&array[i], &array[j]);
...@@ -322,20 +320,16 @@ int partition(int size, int array[size], int first, int last) { ...@@ -322,20 +320,16 @@ int partition(int size, int array[size], int first, int last) {
. . . . . .
```C ```C
void bubble_sort(int size, int array[]) { rien tri_a_bulles(entier tableau[])
for i in [size-1, 1] { pour i de longueur(tableau)-1 à 1:
sorted = true; trié = vrai
for j in [0, i-1] { pour j de 0 à i-1:
if (array[j] > array[j+1]) { si (tableau[j] > tableau[j+1])
swap(array[j], array[j+1]); swap(array[j], array[j+1])
sorted = false; trié = faux
}
} si trié
if (sorted) { retourner
return;
}
}
}
``` ```
# Tri à bulle (4/4) # Tri à bulle (4/4)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment