Work one sweep of Bubble sort at a time. Each sweep is carried out exactly as you write it, so a wrong sweep follows you into the next one — after the last sweep the array is either sorted or it is not.
walk the neighbouring pairs across , swapping a pair when it
is out of order. Answer the highlighted comparison — a swap happens the moment
you click it:
The sweep you have been doing by hand, and the whole sort that calls it. The signatures are given
— write the bodies, then press Check my code. Your bubble_sort
must do its work by calling bubble_sweep. Your code is saved with your run for your
teacher to read beside what you actually did.
void bubble_sweep(int *arr, int len, int i)
void bubble_sort(int *arr, int len)
arr[0] … arr[k-1] are already settled (shown dark). The scan covers the rest: from arr[k] to arr[n-1].&: &arr[k] and &arr[idx]. Step 1 names elements, not addresses, so no & there. If the value is already in place, tick no swap needed.arr[3], &arr[3] or just 3. Enter works like Apply.arr[0] with arr[1], then arr[1] with arr[2], and so on. A pair that is out of order is swapped immediately.arr[7], sweep 1 settles arr[6]. So sweep i stops at arr[7-i] and has one row fewer than the sweep before it.