Loading…
Loading…
Repeatedly steps through the list, swapping adjacent out-of-order pairs so the largest values 'bubble' to the end.
Start bubble sort.
1void bubbleSort(int[] arr) {2 int n = arr.length;3 for (int i = 0; i < n - 1; i++) { // each pass4 for (int j = 0; j < n - i - 1; j++) {5 if (arr[j] > arr[j + 1]) { // adjacent pair out of order6 int temp = arr[j]; // swap them7 arr[j] = arr[j + 1];8 arr[j + 1] = temp;9 }10 }11 // largest now at the end12 }13}