-
Table des matières
-
-
-
-
-
-
-
-
Exemple en Python :
def tri_bulles(liste):
n = len(liste)
for i in range(n):
for j in range(0, n - i - 1):
if liste[j] > liste[j + 1]:
liste[j], liste[j + 1] = liste[j + 1], liste[j]
return liste
print(tri_bulles([5, 2, 9, 1, 3]))
Le tri par sélection cherche le plus petit élément et le place en première position, puis répète l’opération pour le reste de la liste jusqu’à ce que tout soit trié.
Exemple en Python :
def tri_selection(liste):
n = len(liste)
for i in range(n):
min_index = i
for j in range(i + 1, n):
if liste[j] < liste[min_index]:
min_index = j
liste[i], liste[min_index] = liste[min_index], liste[i]
return liste
print(tri_selection([5, 2, 9, 1, 3]))
Algorithme | Avantage | Inconvénient |
---|---|---|
Tri à bulles | Simple à comprendre | Peu efficace pour grandes listes |
Tri par sélection | Moins d’échanges | Toujours O(n²) |