ITPEC FE Subject B April 2024 Question 11
From the answer group below, select the correct combination of answers to be inserted into A through C in the program. Here, the array index starts at 1.
The program sorts the data in ascending order using the selection sort algorithm. The algorithm repeatedly selects the smallest element from the unsorted portion of the array and swaps it with the first element of the unsorted portion until the entire array is sorted.
Answer group
Explanation
A: i + 1, B: <, C: data[i] — each pass scans the unsorted part for its smallest value and swaps it into the first unsorted slot.
On pass i, positions 1 to i - 1 already hold the smallest values in order. minPos starts at i, so the scan only needs the rest of the array: j runs from i + 1 to size (blank A). Ascending order needs the minimum, so a value takes over when data[j] < data[minPos] (blank B). The minimum belongs at position i, so the three-line swap exchanges data[i] with data[minPos] (blank C).
Trace on {12, 11, 13, 5, 6}:
- •
i = 1: minimum 5 at position 4, swap → {5, 11, 13, 12, 6} - •
i = 2: minimum 6 at position 5, swap → {5, 6, 13, 12, 11} - •
i = 3: minimum 11 at position 5, swap → {5, 6, 11, 12, 13} - •
i = 4: minimum 12 is already at position 4, so the array is sorted
Answer (e)
Why not others:
- •(a), (b), (c) and (d) — A as
1lets the scan revisit the sorted prefix, where the smallest value always wins; in (a), pass 2 findsdata[1] = 5and swaps it back out, giving {11, 5, 13, 12, 6} - •(c), (d), (g) and (h) — B as
>selects the maximum on each pass, which sorts in descending order - •(b), (d), (f) and (h) — C as
data[puts the minimum into position i + 1 and never fills positioni + 1] i: pass 1 of (f) gives {12, 5, 13, 11, 6}, leaving 12 in front
Key rule: In selection sort, pass i searches only positions i + 1 to the end and swaps the winner into position i; the comparison operator alone decides ascending or descending order.
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.