ITPEC FE Subject B October 2025 Question 11
ITPEC FE Subject B October 2025 — Question 11 of 20
Modified bubble sort with early termination: fill in the while-loop condition blanks.
The function quickBubble is an optimized bubble sort that stops early when no swaps occur during a pass. The while-loop needs two conditions to work correctly:
- •Blank A — `maxIdx > 0`: The inner
forloop runsifrom1tomaxIdx, comparingarraySorted[i]witharraySorted[i + 1]. For at least one comparison to happen,maxIdxmust be ≥ 1, i.e.maxIdx > 0. After each pass,maxIdxdecreases by 1 (the largest unsorted element "bubbles up" to positionmaxIdx + 1). UsingmaxIdx > 1would skip the final single-pair pass whenmaxIdx = 1.
- •Blank B — `exchange = true`: Before each pass,
exchangeis reset tofalse. If any swap occurs, it becomestrue. The while-loop should continue as long as swaps happened — meaning the array may not yet be sorted. If no swaps occurred (exchange = false), the array is already sorted and the loop exits early.
Why not others:
- b) `maxIdx > 0` and `exchange = false` — would continue only when no swaps occurred, which is the opposite of the intended logic
- c) `maxIdx > 1` and `exchange = true` — maxIdx > 1 would prevent the last necessary pass when only two unsorted elements remain (maxIdx = 1)
- d) `maxIdx > 1` and `exchange = false` — combines both wrong conditions
Key rule: In early-termination bubble sort, the flag tracks whether a swap happened. The loop continues while exchange = true (swaps occurred → array might not be sorted yet). The boundary condition maxIdx > 0 ensures every needed pass executes, including the final single-comparison pass.
AI-generated — may contain errors
The original exam layout is preserved in the image so diagrams, formulas, tables, and code remain accurate.
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.