Exercice 57
Ecrire un algorithme en Python permettant de trier une liste selon l'algorithme du tri par insertion.
Solution
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
def sort(L): for i in range(1, len(L)): current = L[i] for j in range(i - 1, -1, -1): if L[j] > current: L[j], L[j + 1] = L[j + 1], L[j] else: L[j + 1] = current break return L # Exemple L = [41 , 23 , 7 , 31 , 27 , 5 , 2 , 17 , 25 ] print(sort(L)) # affiche: [2, 5, 7, 17, 23, 25, 27, 31, 41] |
Younes Derfoufi
CRMEF OUJDA

![[App intégrée] 2025 Upgraded Vidéoprojecteur 1920 * 1080P FHD 4K Mini Projecteur Portable Dual Contrôle avec Souris Android TV WiFi 6 BT5.2 180° Rotation Compatible avec HDMI/TV Stick/USB](https://www.tresfacile.net/wp-content/uploads/2025/12/Videoprojecteur-1920-1080P-FHD-4K-Mini-Projecteur-Portable-Dual-Control-250x236.png)

1 thought on “Solution Exercice 57: tri par insertion en python”