Skip to content

Selection sort

ordering

Level 1 of 3

Also called Straight selection.

fresh numbers

The test's questions that a run answers, asked again on new numbers whenever you like. The answer, its working and the reason for every other choice come from selection sort's own run on the numbers shown.

choose one

question 1

Selection sort works on 5, 9, 3, 8, 1, 6. Its first pass swaps the 1 into place 1. What is the row after its third pass?

593816
the answer, and why

the answer is B

Pass 1 swaps the 1 and the 5, giving 1, 9, 3, 8, 5, 6. Pass 2 swaps the 3 and the 9, giving 1, 3, 9, 8, 5, 6. Pass 3 swaps the 5 and the 9, giving 1, 3, 5, 8, 9, 6.

if you chose another

  • That is the row after pass 2.
  • That is the row sorted, two passes later.
  • That shifts the 9 and the 8 along to make room for the 5, as insertion sort would. Selection sort swaps the 5 with the 9.

new numbers

question 2

How many comparisons does selection sort make on 6, 2, 7, 1, 5, 3, 4, from start to finish?

6271534
the answer, and why

the answer is B

The six passes compare the 6, 5, 4, 3, 2 and 1 numbers after their places, so 6 + 5 + 4 + 3 + 2 + 1 = 21.

if you chose another

  • That counts the swaps.
  • That is one comparison per pass, but each pass compares every number after its place.
  • That is insertion sort's count on this row.

new numbers

question 3

Selection sort works on each of these rows of 1 to 5. On which does it make the most swaps?

the answer, and why

the answer is C

A pass swaps unless its smallest is already home. On 3, 1, 5, 2, 4 every pass swaps. The row becomes 1, 3, 5, 2, 4, then 1, 2, 5, 3, 4, then 1, 2, 3, 5, 4, then 1, 2, 3, 4, 5. That is 4 swaps, the most 5 numbers can need.

if you chose another

  • Passes 1 and 2 swap the 1 with the 5 and the 2 with the 4, and then all is home, so 2 swaps.
  • Only the passes for places 1 and 3 swap, so 2 swaps.
  • Only the pass for place 4 swaps, so 1 swap.

new numbers