
{filelink=68}
/***** Code de MesExemples.com *******/
class TriABulleDecroissant
{
public static void main(String[] args)
{
// initialiser le tableau à trier
int nombres[] = new int[]{15,58,35,45,150,3, 96};
// le contenu du tableau avant le tri
System.out.println("tableau avant le tri à bulle");
for(int i=0; i < nombres.length; i++)
{
System.out.print(nombres[i] + " ");
}
// Trier le tableau en ordre décroissant à l'aide de l'algorithme de "bubble"
trierAbulle(nombres);
System.out.println("");
// Afficher le résultat du tri
System.out.println("tableau après le tri à bulle");
for(int i=0; i < nombres.length; i++)
{
System.out.print(nombres[i] + " ");
}
}
private static void trierAbulle(int[] nombres)
{
int n = nombres.length;
int temp = 0;
for(int i=0; i < n; i++)
{
for(int j=1; j < (n-i); j++)
{
if(nombres[j-1] < nombres[j])
{
temp = nombres[j-1];
nombres[j-1] = nombres[j];
nombres[j] = temp;
}
}
}
}
}
/*
* tableau avant le tri à bulle
15 58 35 45 150 3 96
tableau après le tri à bulle
150 96 58 45 35 15 3
*/