ITPEC FE Subject B October 2025 Question 11
From the answer group below, select the correct combination of answers to be inserted into A and B in the program. Here, the array index starts at 1.
The bubble sort compares adjacent elements in an array and swaps them if they are out of order. It makes multiple passes through an array. The bubble sort can be modified to stop early if it finds that the array has become sorted. The function quickBubble is modified from conventional bubble sort, which sorts the elements in ascending order, to recognize a sorted array and stop early. For example, the sorting of the unordered array {18, 1, 8, 6, 2, 9, 12, 14, 7, 11} is completed in six passes.
Answer group
Explanation
A: maxIdx > 0, B: exchange = true — keep making passes while there is still a pair to compare and the last pass swapped something.
Each pass compares arraySorted[i] with arraySorted[ for i from 1 to maxIdx, so after a pass the largest remaining value sits at position maxIdx + 1 and maxIdx shrinks by 1. A pass with maxIdx = 1 still does one useful comparison (positions 1 and 2), so the loop must run while maxIdx > 0.
exchange is reset to false at the start of each pass and set to true on any swap. A pass without a swap proves the array is sorted, so the loop continues only while exchange = true. The initial exchange ← true exists so that the first pass runs at all.
On the example {18, 1, 8, 6, 2, 9, 12, 14, 7, 11} the fifth pass leaves the array sorted, the sixth pass makes no swap, and the loop stops: six passes, as the question says.
Answer (a)
Why not others:
- •(b) and (d) —
exchangestarts astrue, soexchange = falsefails at the very first test, the loop body never runs and the array is returned unsorted - •(c) —
maxIdx > 1skips the pass withmaxIdx= 1, the one that compares positions 1 and 2. The six-pass example happens to finish before that point, but{3, 2, 1}stops after one pass as{2, 1, 3}, and{2, 1}is never sorted at all
Key rule: For a loop guarded by a flag, check that the flag's initial value lets the first iteration run, and test the bound on the smallest case, here a two-element array.
Sorting · AI-written, may contain errors
This question comes from an official ITPEC past paper. ITPEC Practice is an independent study tool and is not affiliated with ITPEC. See the official FE past-paper collection or Report an issue.