Exercice 58
Ecrire un algorithme en Python permettant de trier une liste selon l'algorithme du tri à bulles (Bubble Sort).
Solution
|
1 2 3 4 5 6 7 8 9 10 11 12 13 |
# coding: utf-8 def bubble_sort(L): for i in range(len(L)): for j in range(len(L) - 1, i, -1): if L[j] < L[j - 1]: L[j], L[j - 1] = L[j - 1], L[j] return L # Exemple L = [ '17' , '13' , '22' , '43' , '5' , '11' , '19'] print(bubble_sort(L)) # affiche: ['11', '13', '17', '19', '22', '43', '5'] |
Younes Derfoufi
CRMEF OUJDA
Acheter sur Très Facile !
-

Maîtriser la Programmation Python pour le Machine Learning
€ 33,00 Acheter le livre -

Apprendre Python au le Lycée: Seconde, Première, Terminale
€ 14,00 Acheter le livre -
Promo !

Apprendre Python un guide complet et progressif
Le prix initial était : € 11,00.€ 10,00Le prix actuel est : € 10,00. Ajouter au panier
1 thought on “Solution Exercice 58: algorithme de tri à bulle en python”