Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.
Shade in the A or B circle for each question.
Draw a line connecting each item on the left to its match on the right.
| Left column | Right column | |
|---|---|---|
| 1minIndex ← i | A[1, 2, 8, 5] | |
| 2SWAP numbers[i], numbers[minIndex] | BMake the one swap for this pass, putting the minimum at the end of the sorted part | |
| 3Trace of [5, 2, 8, 1] after pass 1: the minimum 1 at index 4 is swapped with the first item, so the sorted part is [1] | C[1, 2, 8, 5] | |
| 4After pass 2 of [1, 2, 8, 5], the minimum of the unsorted part is already in place, so the swap changes nothing and the sorted part grows to two | DThe sorted part grows by one item each pass from the left, so after pass 2 it holds two items and never shrinks. | |
| 5Pass 1 of [4, 3, 1, 2] traced as [3, 4, 1, 2] | EThis swaps only the first two items, as a bubble sort would. Selection sort swaps the first item with the minimum, 1, giving [1, 3, 4, 2]. | |
| 6Trace row for pass 2 shows the sorted part shrinking from [1, 2] to [1] | FAssume the first unsorted item is the smallest at the start of each pass |
Complete each line of code by filling in the missing parts.
Shade in the letter of the line that contains an error.
trace table — A table of the array after each selection sort pass: the minimum of the unsorted part is swapped to the left, so the sorted part grows by one each pass.FOR i ← 1 TO n - 1
minIndex ← i
FOR j ← i + 1 TO nFOR i ← 1 TO n - 1
minIndex ← i
FOR j ← i + 1 TO nIF data[j] < data[minIndex] THEN
minIndex ← j
ENDIFtemp ← data[i]
data[i] ← data[minIndex]
data[minIndex] ← tempStart: [5, 3, 8, 1]
Pass 1: [1, 3, 8, 5]
Pass 2: [1, 3, 8, 5]