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
radhwan.hassine
cours
Commits
a3288fbd
Verified
Commit
a3288fbd
authored
3 years ago
by
orestis.malaspin
Browse files
Options
Downloads
Patches
Plain Diff
added cours 7
parent
2e695f75
No related branches found
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
+74
-5
74 additions, 5 deletions
slides/cours_7.md
with
74 additions
and
5 deletions
slides/cours_7.md
+
74
−
5
View file @
a3288fbd
---
---
title: "Tris"
title: "Tris
et backtracking
"
date: "2021-11-12"
date: "2021-11-12"
patat:
patat:
eval:
eval:
...
@@ -197,7 +197,7 @@ int partition(int size, int array[size], int first, int last) {
...
@@ -197,7 +197,7 @@ int partition(int size, int array[size], int first, int last) {
}
}
```
```
# Tri à bulle (1/
N
)
# Tri à bulle (1/
4
)
## Algorithme
## Algorithme
...
@@ -214,17 +214,86 @@ int partition(int size, int array[size], int first, int last) {
...
@@ -214,17 +214,86 @@ int partition(int size, int array[size], int first, int last) {
*
Plus besoin de le traiter.
*
Plus besoin de le traiter.
*
A chaque parcours on s'arrête un élément plus tôt.
*
A chaque parcours on s'arrête un élément plus tôt.
# Tri à bulle (2/
N
)
# Tri à bulle (2/
4
)
## Exemple
## Exemple


# Tri à bulle (3/
N
)
# Tri à bulle (3/
4
)
## Exercice: écrire l'algorithme
du tri à bulles
## Exercice: écrire l'algorithme
(poster le résultat sur matrix)
. . .
. . .
```
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;
}
}
}
```
# Tri à bulle (3/4)
## Quelle est la complexité du tri à bulles?
. . .
*
Dans le meilleurs des cas:
*
Le tableau est déjà trié: $
\m
athcal{O}(N)$ comparaisons.
*
Dans le pire des cas, $N
\c
dot (N-1)/2
\s
im
\m
athcal{O}(N^2)$:
$$
\s
um_{i=1}^{N-1}i
\m
box{ comparaison et }3
\s
um_{i=1}^{N-1}i
\m
box{ affectations
(swap)}
\R
ightarrow
\m
athcal{O}(N^2).
$$
*
En moyenne, $
\m
athcal{O}(N^2)$ ($N^2/2$ comparaisons).
# L'algorithme du PPCM *récursif* (1/2)
## Rappel de l'algorithme
```
C
// on cherche i*m == j*m (i,j aussi petits que possibles)
int ppcm(int m, int n) {
int mult_m = m, mult_n = n;
while (mult_m != mult_n) {
if (mult_m > mult_n) {
mult_n += n;
} else {
mult_m += m;
}
}
return mult_m;
}
```
. . .
## Écrire l'algorithme *récursif* du PPCM (matrix)
# L'algorithme du PPCM *récursif* (1/2)
```
C
int ppcm(int mult_n, int mult_m, int n, int m) {
if (mult_n < mult_m) {
return ppcm(n + mult_n, mult_m, n, m);
} else if (mult_n > mult_m) {
return ppcm(mult_n, m + mult_m, n, m);
} else {
return mult_n;
}
}
```
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