AlgoBox : Méthode du "tri à bulle"

Présentation de l'algorithme :

Exemple de tri à bulle sur une liste de 10 nombres (générés aléatoirement) compris entre 0 et 20.

Fichier AlgoBox associé : tribulle.alg (faire un clic-droit et utiliser l'option "enregistrer sous" pour télécharger le fichier)


Tester l'algorithme
Cliquer sur ce bouton pour exécuter l'algorithme : 

Résultats

Code de l'algorithme
1   VARIABLES
2     listeatrier EST_DU_TYPE LISTE
3     i EST_DU_TYPE NOMBRE
4     j EST_DU_TYPE NOMBRE
5     temp EST_DU_TYPE NOMBRE
6   DEBUT_ALGORITHME
7     AFFICHER "Avant le tri :"
8     POUR i ALLANT_DE 1 A 10
9       DEBUT_POUR
10      listeatrier[i] PREND_LA_VALEUR floor(random()*20+1)
11      AFFICHER listeatrier[i]
12      AFFICHER " "
13      FIN_POUR
14    POUR i ALLANT_DE 1 A 9
15      DEBUT_POUR
16      POUR j ALLANT_DE 1 A 9
17        DEBUT_POUR
18        SI (listeatrier[j+1]<listeatrier[j]) ALORS
19          DEBUT_SI
20          temp PREND_LA_VALEUR listeatrier[j]
21          listeatrier[j] PREND_LA_VALEUR listeatrier[j+1]
22          listeatrier[j+1] PREND_LA_VALEUR temp
23          FIN_SI
24        FIN_POUR
25      FIN_POUR
26    AFFICHER " "
27    AFFICHER "Après le tri :"
28    POUR i ALLANT_DE 1 A 10
29      DEBUT_POUR
30      AFFICHER listeatrier[i]
31      AFFICHER " "
32      FIN_POUR
33  FIN_ALGORITHME