Bubble sort algorithm demonstration diagram:
public static void bubbleSort(int[] array) { //Choose one of the next two options///Method 1 for (int i = array.length - 1; i > 0; i--) { for (int j = 0; j < i; j++) { if (array[j] > array[j + 1]) { Sort.swap(array, j, j + 1);//Exchange j and j+1 } } } / //Method 2 for(int i=0;i<array.length;i++){ for(int j=0;j<array.length-1-i;j++){ if(array[j]>array[j+1]) { Sort.swap(array, j, j+1);//Swap j and j+1 } } } }The above is the entire content of this article. I hope it will be helpful to everyone in understanding the bubble sort algorithm in Java.