Qu'est-ce que le tri à bulles?
Le tri à bulles est un algorithme simple qui compare le premier élément du tableau au suivant. Si l'élément actuel du tableau est numériquement plus grand que le suivant, les éléments sont permutés. De même, l'algorithme parcourra tout l'élément du tableau.
Dans ce tutoriel, nous allons créer un programme JAVA pour implémenter Bubble Sort. Vérifiez la sortie du code qui vous aidera à comprendre la logique du programme
package com.guru99;classe publique BubbleSort {public static void main (String [] args){int arr [] = {860,8 200,9};System.out.println ("--- Array BEFORE Bubble Sort ---");printArray (arr);bubbleSort (arr); // tri des éléments du tableau à l'aide du tri par bullesSystem.out.println ("--- Array AFTER Bubble Sort ---");printArray (arr);}bubbleSort statique vide (tableau int []){int n = array.length;int temp = 0;for (int i = 0; itableau [j]){// permuter les élémentstemp = tableau [j-1];tableau [j-1] = tableau [j];tableau [j] = temp;System.out.println (array [j] + "est supérieur à" + array [j-1]);System.out.println ("Swapping Elements: New Array After Swap");printArray (tableau);}}}}static void printArray (int [] array) {pour (int i = 0; i Production:
860 8 200 9Sort Pass Number 1Comparing 860 and 8860 is greater than 8Swapping Elements: New Array After Swap8 860 200 9Comparing 860 and 200860 is greater than 200Swapping Elements: New Array After Swap8 200 860 9Comparing 860 and 9860 is greater than 9Swapping Elements: New Array After Swap8 200 9 860Sort Pass Number 2Comparing 8 and 200Comparing 200 and 9200 is greater than 9Swapping Elements: New Array After Swap8 9 200 860Sort Pass Number 3Comparing 8 and 9Sort Pass Number 4---Array AFTER Bubble Sort---8 9 200 860