/* Tri a bulle d'un tableau d'entiers */ public class TriBulle { /* Fonction d'affichage des valeurs contenues */ /* dans un tableau de int */ static void affichageTableau(int [] t) { for ( int i = 0 ; i < t.length ; i++ ) { Ecran.formater("%4d",t[i]); } Ecran.sautDeLigne(); } /* Fonction de creation d'un tableau de 10 int */ /* initialise avec des valeurs tirees au sort */ /* dans l'intervalle [0,1000[ */ static int [] initRand() { int [] t = new int[10]; for ( int i = 0 ; i < t.length ; i++ ) { t[i] =(int) (Math.random()*1000.0); } return t; } /////////////////////////////////////////////////// /* Fonction de tri à bulle par ordre croissant */ /* des valeurs contenues dans un tableau d'int */ /* t : Le tableau d'int à trier */ /* par ordre croissant */ static void triBulle(int [] t) { int n = t.length; for ( int i = 0 ; i < n-1 ; i++ ) { for ( int j = 0 ; j < n-i-1 ; j++ ) { if ( t[j] > t[j+1] ) { int aux = t[j]; t[j] = t[j+1]; t[j+1] = aux; } } } } /////////////////////////////////////////////////// /* Programme principal */ public static void main(String [] args) { int [] t = initRand(); Ecran.afficherln("Tableau initial"); affichageTableau(t); triBulle(t); Ecran.afficherln("Tableau trié"); affichageTableau(t); } }