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

tri par insertion

parent 02b08076
No related branches found
No related tags found
No related merge requests found
Pipeline #14788 passed
...@@ -398,9 +398,32 @@ $$ ...@@ -398,9 +398,32 @@ $$
\mathcal{O}(N^3). \mathcal{O}(N^3).
$$ $$
. . . # Ordres de grandeur
\begin{table}[!h]
\begin{center}
\caption{Valeurs approximatives de quelques fonctions usuelles de complexité.}
\medskip
\begin{tabular}{|c|c|c|c|c|}
\hline
$\log_2(N)$ & $\sqrt{N}$ & $N$ & $N\log_2(N)$ & $N^2$ \\
\hline\hline
$3$ & $3$ & $10$ & $30$ & $10^2$ \\
\hline
$6$ & $10$ & $10^2$ & $6\cdot 10^2$ & $10^4$ \\
\hline
$9$ & $31$ & $10^3$ & $9\cdot 10^3$ & $10^6$ \\
\hline
$13$ & $10^2$ & $10^4$ & $1.3\cdot 10^5$ & $10^8$ \\
\hline
$16$ & $3.1\cdot 10^2$ & $10^5$ & $1.6\cdot 10^6$ & $10^{10}$ \\
\hline
$19$ & $10^3$ & $10^6$ & $1.9\cdot 10^7$ & $10^{12}$ \\
\hline
\end{tabular}
\end{center}
\end{table}
<https://fr.wikipedia.org/wiki/Analyse_de_la_complexit%C3%A9_des_algorithmes>
# Quelques exercices (1/3) # Quelques exercices (1/3)
...@@ -475,5 +498,50 @@ $$ ...@@ -475,5 +498,50 @@ $$
swaps})=\mathcal{O}(N^2). swaps})=\mathcal{O}(N^2).
$$ $$
# Tri par insertion (1/N)
## But
* trier un tableau par ordre croissant
## Algorithme
Prendre un élément du tableau et le mettre à sa place parmis les éléments déjà
triés du tableau.
![Tri par insertion d'un tableau d'entiers](figs/tri_insertion.svg)
# Tri par insertion (2/N)
## Exercice: Proposer un algorithme
. . .
```C
void tri_insertion(int size, int tab[size]) {
for (int i = 1; i < size; i++) {
int pos = i;
int tmp = tab[i];
while (pos > 0 && tab[pos - 1] > tmp) {
tab[pos] = tab[pos - 1];
pos = pos - 1;
}
tab[pos] = tmp;
}
}
```
# Tri par insertion (2/N)
## Question: Quelle est la complexité?
. . .
* Parcous de tous les éléments (ordre $N$); placer (ordre $N$).
* Moyenne: $\mathcal{O}(N^2)$.
. . .
* Pire des cas, liste triée à l'envers: $\mathcal{O}(N^2)$,
* Meilleurs des cas, liste déjà triée: $\mathcal{O}(N)$,
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment