From 057db02cd49f955b214e8b78731cae9c4e2ae4c8 Mon Sep 17 00:00:00 2001 From: Orestis <orestis.malaspinas@pm.me> Date: Thu, 28 Oct 2021 17:31:12 +0200 Subject: [PATCH] tri par insertion --- slides/cours_6.md | 72 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 70 insertions(+), 2 deletions(-) diff --git a/slides/cours_6.md b/slides/cours_6.md index 5305570..014dfd0 100644 --- a/slides/cours_6.md +++ b/slides/cours_6.md @@ -398,9 +398,32 @@ $$ \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) @@ -475,5 +498,50 @@ $$ 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 (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)$, -- GitLab