ITPEC FE Subject B October 2025 Question 11

Source exam: ITPEC FE Subject B October 2025Topic: Array Manipulation

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 for loop runs i from 1 to maxIdx, comparing arraySorted[i] with arraySorted[i + 1]. For at least one comparison to happen, maxIdx must be ≥ 1, i.e. maxIdx > 0. After each pass, maxIdx decreases by 1 (the largest unsorted element "bubbles up" to position maxIdx + 1). Using maxIdx > 1 would skip the final single-pair pass when maxIdx = 1.
  • Blank B — `exchange = true`: Before each pass, exchange is reset to false. If any swap occurs, it becomes true. 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.