The given array is arr = {3, 4, 5, 2, 1}. The number of iterations in bubble…

2023

The given array is arr = {3, 4, 5, 2, 1}. The number of iterations in bubble sort and selection sort respectively are __________

  1. A.

    5 and 4

  2. B.

    4 and 5

  3. C.

    2 and 4

  4. D.

    2 and 5

Attempted by 270 students.

Show answer & explanation

Correct answer: A

Concept: Two different sorting algorithms use two different loop-termination rules, so "number of iterations" means something different for each. An optimised (swap-flag) bubble sort keeps making left-to-right adjacent-swap passes and stops only once an entire pass completes with zero swaps — that final zero-swap pass is itself counted as one iteration. Selection sort, by contrast, dedicates one pass to permanently fixing exactly one array position (the minimum of the still-unplaced elements); once the first n − 1 positions are each fixed by their own pass, the single remaining position must already hold the correct value, so selection sort always needs exactly n − 1 passes for n elements — no confirmatory extra pass is possible.

Application:

Bubble sort trace on {3, 4, 5, 2, 1}:

  1. Pass 1 — compare and swap adjacent pairs left to right: (3,4) no swap, (4,5) no swap, (5,2) swap, (5,1) swap → array becomes 3, 4, 2, 1, 5 (swaps occurred, so sorting is not yet confirmed done).

  2. Pass 2 — (3,4) no swap, (4,2) swap, (4,1) swap, (4,5) no swap → array becomes 3, 2, 1, 4, 5 (swaps occurred).

  3. Pass 3 — (3,2) swap, (3,1) swap, (3,4) no swap, (4,5) no swap → array becomes 2, 1, 3, 4, 5 (swaps occurred).

  4. Pass 4 — (2,1) swap, (2,3) no swap, (3,4) no swap, (4,5) no swap → array becomes 1, 2, 3, 4, 5 (swaps occurred).

  5. Pass 5 — (1,2) no swap, (2,3) no swap, (3,4) no swap, (4,5) no swap → zero swaps this pass, so the algorithm confirms the array is fully ordered and stops. This zero-swap confirmation pass is still one full iteration of the outer loop.

Selection sort trace on {3, 4, 5, 2, 1}:

  1. Pass 1 — scan the whole unplaced range {3, 4, 5, 2, 1} for its minimum (1) and swap it into position 1 → 1, 4, 5, 2, 3.

  2. Pass 2 — scan the remaining unplaced range {4, 5, 2, 3} for its minimum (2) and swap it into position 2 → 1, 2, 5, 4, 3.

  3. Pass 3 — scan the remaining unplaced range {5, 4, 3} for its minimum (3) and swap it into position 3 → 1, 2, 3, 4, 5.

  4. Pass 4 — scan the remaining unplaced range {4, 5} for its minimum (4); it is already in its correct position, so no swap is needed, but the pass still runs → 1, 2, 3, 4, 5.

After 4 passes, only one element remains in the unplaced range (the last position) — it must already hold the correct value, so the loop stops here without a 5th pass.

Cross-check: Both traces land on the same fully sorted array 1, 2, 3, 4, 5, confirming the simulation is consistent. The pass counts differ specifically because of each algorithm's own stopping rule: bubble sort's swap-flag optimisation needs an explicit no-swap pass to know it is safe to stop, while selection sort's stopping point is guaranteed structurally the moment n − 1 positions are individually fixed — it never needs that extra confirming pass. This is also why some references quote bubble sort's worst-case pass count as n − 1 (an un-optimised version that always runs exactly n − 1 outer-loop iterations with no early exit): the pass count used here specifically reflects the optimised, swap-flag version that keeps checking until a pass finds nothing left to swap.

Result: So bubble sort takes 5 iterations and selection sort takes 4 iterations for this array.

Explore the full course: Coding For Placement