Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cours
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
aurelien.boyer
cours
Commits
080abb51
Verified
Commit
080abb51
authored
2 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
updated pseudocodes to rfench
parent
1f67f0fa
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
slides/cours_7.md
+41
-47
41 additions, 47 deletions
slides/cours_7.md
with
41 additions
and
47 deletions
slides/cours_7.md
+
41
−
47
View file @
080abb51
...
...
@@ -22,11 +22,11 @@ double tab[N];
. . .
```
C
double m
oyenne
= 0.0;
double m
ean
= 0.0;
for (int i = 0; i < N; ++i) { // N assignations
m
oyenne
+= tab[i]; // N additions
m
ean
+= tab[i]; // N additions
}
m
oyenne
/= N; // O(N)
m
ean
/= N; // O(N)
```
. . .
...
...
@@ -36,12 +36,12 @@ moyenne /= N; // O(N)
. . .
```
C
double m
oyenne
= moyenne(N, tab); // O(N)
double
ecart
= 0.0;
double m
ean
= moyenne(N, tab); // O(N)
double
dev
= 0.0;
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)
...
...
@@ -59,7 +59,7 @@ triés du tableau.
# 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
## Pseudocode: quicksort
```
C
void quicksort(array, low, high) {
if (more than 1 elems) {
pivot_ind = partition(array, low, high);
if (something left of pivot)
quicksort(array, low, pivot_ind - 1);
if (something right of pivot)
quicksort(array, pivot_ind + 1, high);
}
}
```
python
rien
quicksort
(
entier
tableau
[],
ind_min
,
ind_max
)
si
(
longueur
(
tab
)
>
1
)
ind_pivot
=
partition
(
array
,
ind_min
,
ind_max
)
si
(
longueur
(
tableau
[
0
:
ind_pivot
-
1
])
!=
0
)
quicksort
(
tableau
,
ind_min
,
pivot_ind
-
1
);
si
(
longueur
(
tableau
[
ind_pivot
+
1
:
ind_max
-
1
])
!=
0
)
quicksort
(
tableau
,
ind_pivot
+
1
,
ind_max
)
```
# Tri rapide ou quicksort (5/8)
...
...
@@ -184,20 +182,20 @@ void quicksort(array, low, high) {
## Pseudocode: partition
```
C
i
nt partition(
array, low, high) {
pivot = array[
high
]; // choix arbitraire
i =
low
;
j =
high
-1;
whil
e i < j
{
en remontant i trouver le premier élément > pivot;
en descendant j trouver le premier élément < pivot;
swap
(array[i], array[j]);
// les plus grands à droite
// mettre les plus petits à gauche
}
e
nt
ier
partition(
entier tableau[], entier ind_min, entier ind_max)
pivot = array[
ind_max
]; // choix arbitraire
i =
ind_min
;
j =
ind_max
-1;
tant qu
e i < j
:
en remontant i trouver le premier élément > pivot;
en descendant j trouver le premier élément < pivot;
échanger
(array[i], array[j]);
// les plus grands à droite
// mettre les plus petits à gauche
// on met le pivot "au milieu"
swap
(array[i], array[pivot]);
return i; // on retourne l'indice pivot
échanger
(array[i], array[pivot]);
ret
o
urn
e
i; // on retourne l'indice pivot
}
```
...
...
@@ -259,10 +257,10 @@ int partition(int size, int array[size], int first, int last) {
int i = first - 1, j = last;
do {
do {
i
++
;
i
+= 1
;
} while (array[i] < pivot && i < j);
do {
j
--
;
j
-= 1
;
} while (array[j] > pivot && i < j);
if (j > i) {
swap(&array[i], &array[j]);
...
...
@@ -322,20 +320,16 @@ int partition(int size, int array[size], int first, int last) {
. . .
```
C
void bubble_sort(int size, int array[]) {
for i in [size-1, 1] {
sorted = true;
for j in [0, i-1] {
if (array[j] > array[j+1]) {
swap(array[j], array[j+1]);
sorted = false;
}
}
if (sorted) {
return;
}
}
}
rien tri_a_bulles(entier tableau[])
pour i de longueur(tableau)-1 à 1:
trié = vrai
pour j de 0 à i-1:
si (tableau[j] > tableau[j+1])
swap(array[j], array[j+1])
trié = faux
si trié
retourner
```
# Tri à bulle (4/4)
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment