Demo lesson: try every tab (Learn, Watch, Predict, Practice) as often as you like. It doesn't use up your 3 free topics.
Sign up free to unlock all 99Sort 8 numbers. On every pass, the biggest unsorted value bubbles to the end.
1void bubbleSort(int[] a) {2int n = a.length;3for (int i = 0; i < n - 1; i++) {4boolean swapped = false;5for (int j = 0; j < n - 1 - i; j++) {6if (a[j] > a[j + 1]) {7swap(a, j, j + 1);8swapped = true;9}10}11if (!swapped) break;12}13}
Variables
Work done
Shortcuts: Space play/pause · ← → step · M sound