logo

Classificação por bolha em Java

Podemos criar um programa java para classificar elementos de array usando bubble sort. O algoritmo de classificação por bolha é conhecido como o algoritmo de classificação mais simples.

No algoritmo de classificação por bolha, a matriz é percorrida do primeiro ao último elemento. Aqui, o elemento atual é comparado com o próximo elemento. Se o elemento atual for maior que o próximo elemento, ele será trocado.

 public class BubbleSortExample { static void bubbleSort(int[] arr) { int n = arr.length; int temp = 0; for(int i=0; i <n; i++){ for(int j="1;" arr[j]){ swap elements temp="arr[j-1];" arr[j-1]="arr[j];" arr[j]="temp;" } public static void main(string[] args) { int arr[]="{3,60,35,2,45,320,5};" system.out.println('array before bubble sort'); i="0;" < arr.length; system.out.print(arr[i] + ' '); system.out.println(); bubblesort(arr); sorting array using sort after pre> <p>Output:</p> <pre> Array Before Bubble Sort 3 60 35 2 45 320 5 Array After Bubble Sort 2 3 5 35 45 60 320 </pre></n;>