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[] a) {2 int n = a.length;3 for (int i = 0; i < n - 1; i++) {4 for (int j = 0; j < n - i - 1; j++) {5 if (a[j] > a[j + 1]) {6 int t = a[j];7 a[j] = a[j + 1];8 a[j + 1] = t;9 }10 }11 // a[n - i - 1] is now sorted12 }13}