Skip to content

Selection sort

ordering

Level 1 of 3

Also called Straight selection.

test yourself

Six questions, four to pick from and two for paper. Each answer stays hidden until you open it.

fresh numbers: the questions a run answers, asked again on new numbers

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.

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.

question 3

On a row back to front, selection sort makes few swaps, 5 on 10 numbers. Which statement about its comparisons on such a row is both true and tight?

the answer, and why

the answer is A

Every pass looks at every number left, so a row back to front costs n(n−1)2\frac{n(n - 1)}{2}, as every row does. That is Θ(n2)\Theta(n^2), by the learn page's first ladder.

this tests best, worst and average, on the learn page

if you chose another

  • Its swaps stay under nn, but every pass still looks at every number left.
  • No fixed multiple of nn stays above it. n(n−1)2\frac{n(n - 1)}{2} is nn times half of n−1n - 1, and half of n−1n - 1 passes any fixed number. 10 times nn is passed from 22 numbers on, 231 against 220.
  • True but not tight. n(n−1)2\frac{n(n - 1)}{2} is nn times half of n−1n - 1, and half of n−1n - 1 is at least 1 from 3 numbers on. The tight bound needs a floor of n2n^2.

question 4

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.

pen and paper

question 5about 15 minutes

Every row of nn numbers costs selection sort n(n−1)2\frac{n(n - 1)}{2} comparisons. Show it, and show that this count is Θ(n2)\Theta(n^2).

  1. Count one pass's comparisons, and say why the numbers do not change that.
  2. Add up the passes and reach a formula.
  3. Find a ceiling and a floor with the same term, and name the tight bound.
a hint

Count the numbers each pass looks at, then add 1 + 2 + ... up to n−1n - 1.

a worked answer

The pass for place kk compares each of the n−kn - k numbers after it once, whatever they are. The n−1n - 1 passes make (n−1)+(n−2)+⋯+1(n - 1) + (n - 2) + \dots + 1, which is n(n−1)2\frac{n(n - 1)}{2}. That is n22−n2\frac{n^2}{2} - \frac{n}{2}: at most n22\frac{n^2}{2}, and at least n24\frac{n^2}{4} from 2 numbers on, so Θ(n2)\Theta(n^2). For 4 numbers, 3 + 2 + 1 makes 6.

Why does every row of nn numbers take n(n−1)2\frac{n(n - 1)}{2} comparisons?
  1. the move: count one pass

    the line:
    the pass that fills place kmakes n−k comparisons\begin{gathered} \text{the pass that fills place } k \\ \text{makes } n - k \text{ comparisons} \end{gathered}

    why it is allowed: Each of the n−kn - k numbers after place kk is compared once. On 5, 8, 3, 9, 4, pass 2 compares 3 numbers, 5 less 2. Their order never changes how many.

  2. the move: count the passes

    the line:
    there are n−1 passes,for places 1 to n−1\begin{gathered} \text{there are } n - 1 \text{ passes,} \\ \text{for places 1 to } n - 1 \end{gathered}

    why it is allowed: Once places 1 to n−1n - 1 hold the smallest numbers, the one left is the biggest, already at place nn.

  3. the move: add up the passes

    the line:
    C=(n−1)+(n−2)+⋯+1C = (n - 1) + (n - 2) + \dots + 1

    why it is allowed: CC is the comparisons on any row of nn numbers, line 1 added over line 2's passes. For 4 numbers, 3 + 2 + 1.

  4. the move: add them the other way round

    the line:
    C=1+2+⋯+(n−1)C = 1 + 2 + \dots + (n - 1)

    why it is allowed: The same numbers in the other order. For 4 numbers, 1 + 2 + 3 is 3 + 2 + 1, 6.

  5. the move: use the page on adding up, for a sum that stops at n - 1

    the line:
    C=(n−1)((n−1)+1)2C = \frac{(n - 1)\bigl((n - 1) + 1\bigr)}{2}
    where does this come from?
    question 1 of the page adding up 1+2+⋯+n1 + 2 + \dots + nFit two staircases of squares together. What shape do they make?
    1. the move: draw the sum as a staircase

      the line:
      S=1+2+⋯+nS = 1 + 2 + \dots + n

      why it is allowed: Let SS be the sum of the whole numbers from 1 to nn. Draw it as a staircase, a column for each number: 1 square, then 2, and so on up to nn. For 5, columns of 1, 2, 3, 4 and 5 hold 15 squares.

    2. the move: take two staircases

      the line:
      2S=2(1+2+⋯+n)2S = 2(1 + 2 + \dots + n)

      why it is allowed: Two staircases hold twice the squares. Doubling both sides of line 1 keeps it true.

    3. the move: fit them into a rectangle

      the line:
      2S=n(n+1)2S = n(n + 1)
      Blue stands, yellow hangs. Two staircases of 1 to 5 squares fill a rectangle 5 wide and 6 tall, 30 squares. So 1 + 2 + 3 + 4 + 5 is 5 times 6, halved, which is 15.

      why it is allowed: Stand one staircase with its tallest column on the left. Hang the other upside down from the top, its tallest column on the right, as in the picture. The first column holds nn standing and 1 hanging. Each column to the right, the standing one loses a square and the hanging one gains one. So every column holds n+1n + 1, and the nn columns fill a rectangle nn wide and n+1n + 1 tall.

    4. the move: halve both sides

      the line:
      S=n(n+1)2S = \frac{n(n + 1)}{2}

      why it is allowed: Halving both sides of line 3 keeps it true and leaves one SS. For 5, half of 30 is 15.

    the whole page: adding up 1+2+⋯+n1 + 2 + \dots + n

    why it is allowed: Line 4's sum ends at n−1n - 1. The box above shows 1 + 2 + ... + 5 is 5 times 6, halved, and the same for any last number. For 4 numbers, 3 times 4, halved, is 6.

  6. the move: tidy the bracket

    the line:
    C=(n−1)n2C = \frac{(n - 1)n}{2}

    why it is allowed: One more than n−1n - 1 is nn.

  7. the move: write the product the other way round

    the line:
    C=n(n−1)2C = \frac{n(n - 1)}{2}

    why it is allowed: The same product.

  8. the move: multiply out the bracket

    the line:
    C=n2−n2C = \frac{n^2 - n}{2}

    why it is allowed: nn lots of nn make n2n^2, and nn lots of 1 make nn. For 4 numbers, 4 times 3 is 12, which is 16 less 4.

  9. the move: halve each part

    the line:
    C=n22−n2C = \frac{n^2}{2} - \frac{n}{2}

    why it is allowed: Half of a difference is the difference of the halves.

  10. the move: a ceiling

    the line:
    C≤n22C \le \frac{n^2}{2}

    why it is allowed: Line 9 takes something away from n22\frac{n^2}{2}. For 4 numbers, 6 is at most 8, half of 16.

  11. the move: read it as a bound

    the line:
    C∈O(n2)C \in O(n^2)

    why it is allowed: By line 10, CC stays under a fixed multiple of n2n^2, a half.

  12. the move: a fact about n

    the line:
    n≤n22 for n≥2n \le \frac{n^2}{2} \text{ for } n \ge 2

    why it is allowed: Now a floor. n22\frac{n^2}{2} is nn times half of nn, and half of nn is at least 1 from 2 numbers on. For 10 numbers, 10 is at most 50.

  13. the move: a fact for the floor, halving both sides

    the line:
    n2≤n24 for n≥2\frac{n}{2} \le \frac{n^2}{4} \text{ for } n \ge 2
    nnn2\frac{n}{2}n24\frac{n^2}{4}
    211
    424
    10525
    100502,500
    Equal at 2 numbers, then the quarter of n squared pulls ever further ahead.

    why it is allowed: Line 12 with both sides halved. For 10 numbers, 5 is at most 25, so line 14 can take away 25 instead of line 9's 5, a part made of n2n^2 alone.

  14. the move: a floor

    the line:
    C≥n22−n24 for n≥2C \ge \frac{n^2}{2} - \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: For 10 numbers, 45 is at least 50 less 25. Line 9 takes away n2\frac{n}{2}; line 13's n24\frac{n^2}{4} is at least that, and taking away more leaves less.

  15. the move: tidy the floor

    the line:
    C≥n24 for n≥2C \ge \frac{n^2}{4} \text{ for } n \ge 2

    why it is allowed: Half less a quarter is a quarter.

  16. the move: read it as a bound

    the line:
    C∈Ω(n2)C \in \Omega(n^2)
    nnfloor, n24\frac{n^2}{4}CCceiling, n22\frac{n^2}{2}
    2112
    4468
    10254550
    1002,5004,9505,000
    From 2 numbers on, CC sits between a quarter and a half of n2n^2. That squeeze is Θ(n2)\Theta(n^2).

    why it is allowed: By line 15, CC stays at or above a fixed multiple of n2n^2, a quarter, from 2 numbers on. A floor need only hold for rows long enough; at 1 number CC is 0.

  17. the move: a ceiling and a floor together

    the line:
    C∈Θ(n2)C \in \Theta(n^2)

    why it is allowed: Lines 16 and 11 bound CC by the same growth, n2n^2.

try it with a small nn: at n=4n = 4, line 3 gives C=6C = 6, which you can work out by hand

this tests where the formula comes from, on the learn page

what a full answer includes
  • the pass for place k makes n - k comparisons on every row
  • n - 1 passes, so the sum (n - 1) + (n - 2) + ... + 1
  • the sum reached as n(n - 1)/2, with a reason
  • a ceiling and a floor of n squared, and the tight bound named

question 6about 15 minutes

On a slow store, writing costs far more than comparing: swapping two records takes as long as 100 comparisons. A shuffled list of nn different records is sorted with selection sort. For 1,000 records, take the harmonic sum H(n)H(n) as about 7.49.

  1. How many swaps does it make, on average? Show your working.
  2. Counting each swap as 100 comparisons, what does the whole sort cost on average for 1,000 records?
  3. Which part of that cost grows faster as the list grows, and what does that say about choosing selection sort?
a hint

A pass swaps unless its smallest is already home; ask how likely that is when the numbers left are a shuffle.

a worked answer

The pass for place kk swaps unless the smallest of the n−k+1n - k + 1 numbers left is already home. Those numbers are still a shuffle, so that happens with chance 1n−k+1\frac{1}{n - k + 1}. A tally per pass averages to its chance of a swap, and the averages add up. So the swaps average n−H(n)n - H(n), about 1,000 less 7.49, which is 992.51, for 1,000 records. The comparisons are 499,500 on every list of 1,000, by question 5. The swaps cost about 100 times 992.51, which is 99,251, so the whole cost is about 499,500 plus 99,251, about 598,751. The swaps stay under nn while the comparisons grow like n2n^2, so the comparisons take over on long lists. Selection sort's few swaps pay only when each costs a lot and the list is short.

How many swaps does a shuffled row of different numbers take, on average?
  1. the move: when a pass swaps

    the line:
    the pass for place kswaps unless the smallestof places k to n alreadystands at place k\begin{gathered} \text{the pass for place } k \\ \text{swaps unless the smallest} \\ \text{of places } k \text{ to } n \text{ already} \\ \text{stands at place } k \end{gathered}

    why it is allowed: By line 1 of the learn page's ladder on the most swaps, a pass swaps its smallest in unless it is already home. The numbers not yet placed stand at places kk to nn, which is n−k+1n - k + 1 places. Places 2 to 5 are 4 places, 5 less 2, plus 1.

  2. the move: what is left is still a shuffle

    the line:
    after a pass, the numbers leftstand in every orderequally often\begin{gathered} \text{after a pass, the numbers left} \\ \text{stand in every order} \\ \text{equally often} \end{gathered}
    shufflethe 1 at placeafter pass 1places 2 and 3
    1, 2, 311, 2, 32, 3
    2, 1, 321, 2, 32, 3
    3, 2, 131, 2, 32, 3
    1, 3, 211, 3, 23, 2
    3, 1, 221, 3, 23, 2
    2, 3, 131, 3, 23, 2
    Pass 1 on each shuffle of 1, 2 and 3, grouped by what it leaves. Each order of the 2 and the 3 comes from three shuffles, one for each place the 1 started at.

    why it is allowed: Undo pass 1's swap and the 1 goes back where it started. So each order of the rest comes from one shuffle per place the 1 could start at, nn in all. In the picture, 3, 2 comes from 1, 3, 2, from 3, 1, 2 and from 2, 3, 1. So the next pass meets a fresh shuffle, one number shorter, and the same holds for it.

  3. the move: the chance of no swap

    the line:
    the pass for place kfinds its smallest at homewith chance 1n−k+1\begin{gathered} \text{the pass for place } k \\ \text{finds its smallest at home} \\ \text{with chance } \frac{1}{n - k + 1} \end{gathered}
    1 at place 11321232 shuffles1 at place 32313212 shuffles=1 at place 22133122 shuffles=
    Each arrow trades the two places underlined in red, pairing up the shuffles as they stand before sorting, and trading back undoes it. Each place holds the 1 in 2 of the 6 shuffles.

    why it is allowed: By lines 1 and 2 the n−k+1n - k + 1 numbers left are a shuffle. In 2, 3, 1, trading places 1 and 3 gives 1, 3, 2, and trading back undoes it. So each order with their smallest at one place pairs with one with it at the other. So it stands at each place equally often, and at place kk, the first, in 1 of every n−k+1n - k + 1 orders. In the picture the 1 is at each place in 2 of the 6 shuffles, a third.

  4. the move: the swaps are a sum of tallies

    the line:
    the swaps are a sum of tallies,one per pass, each 1 or 0\begin{gathered} \text{the swaps are a sum of tallies,} \\ \text{one per pass, each 1 or 0} \end{gathered}

    why it is allowed: Each pass gets a tally, 1 when it swaps and 0 when not. Adding a row's tallies counts its swaps.

  5. the move: average tally by tally

    the line:
    the average swapsare the sum ofthe tallies’ averages\begin{gathered} \text{the average swaps} \\ \text{are the sum of} \\ \text{the tallies' averages} \end{gathered}
    shufflepass 1pass 2swaps
    1, 2, 3000
    1, 3, 2011
    2, 1, 3101
    2, 3, 1112
    3, 1, 2112
    3, 2, 1101
    in all437
    average4 over 63 over 67 over 6
    Each pass's tally on every shuffle of 1, 2 and 3, and each row's swaps.

    why it is allowed: Total the swaps column, or total each pass's column and add. Each tally is counted once either way, and dividing by the shuffles keeps them equal. In the picture, 4 and 3 make 7, and 4 over 6 and 3 over 6 make 7 over 6.

  6. the move: a tally's average is its chance

    the line:
    the tally of the passfor place k averages toits chance of a swap\begin{gathered} \text{the tally of the pass} \\ \text{for place } k \text{ averages to} \\ \text{its chance of a swap} \end{gathered}

    why it is allowed: A column of 1s and 0s averages to its share of 1s. In line 5's picture pass 1 swaps in 4 of the 6 shuffles, two thirds, and pass 2 in 3, a half.

  7. the move: a swap or none

    the line:
    that chance is1−1n−k+1\begin{gathered} \text{that chance is} \\ 1 - \frac{1}{n - k + 1} \end{gathered}

    why it is allowed: A pass either swaps or not, so the two chances add to 1, and line 3 gives the chance of none. Two thirds is 1 less a third, and a half is 1 less a half.

  8. the move: add up the passes

    the line:
    E=(1−1n)+(1−1n−1)+⋯+(1−12)\begin{gathered} E = \left(1 - \frac{1}{n}\right) + \left(1 - \frac{1}{n - 1}\right) \\ + \dots + \left(1 - \frac{1}{2}\right) \end{gathered}

    why it is allowed: EE is the swaps averaged over every shuffle of nn different numbers. By lines 5 to 7, place 1 adds 1−1n1 - \frac{1}{n}, down to 1−121 - \frac{1}{2} for place n−1n - 1. For 3 numbers, two thirds plus a half, 7 over 6.

  9. the move: take the ones out

    the line:
    E=(n−1)−(1n+1n−1+⋯+12)\begin{gathered} E = (n - 1) \\ - \left(\frac{1}{n} + \frac{1}{n - 1} + \dots + \frac{1}{2}\right) \end{gathered}

    why it is allowed: n−1n - 1 brackets, each 1 less a fraction. The ones make n−1n - 1, and every fraction is taken away.

  10. the move: add the fractions the other way round

    the line:
    E=(n−1)−(12+13+⋯+1n)E = (n - 1) - \left(\frac{1}{2} + \frac{1}{3} + \dots + \frac{1}{n}\right)

    why it is allowed: The same fractions in the other order.

  11. the move: name the sum

    the line:
    E=(n−1)−(H(n)−1)E = (n - 1) - \bigl(H(n) - 1\bigr)

    why it is allowed: 1, plus a half, and on up to 1 over nn, is the harmonic sum, H(n)H(n). This sum lacks its first 1.

  12. the move: open the bracket

    the line:
    E=(n−1)−H(n)+1E = (n - 1) - H(n) + 1

    why it is allowed: Taking away H(n)H(n) less 1 is taking away H(n)H(n) and giving the 1 back.

  13. the move: put the 1 next to the n - 1

    the line:
    E=(n−1)+1−H(n)E = (n - 1) + 1 - H(n)

    why it is allowed: Adding and taking away give the same in either order.

  14. the move: collect the ones

    the line:
    E=n−H(n)E = n - H(n)

    why it is allowed: (n−1)+1(n - 1) + 1 is nn. For 3 numbers, 3 less about 1.83 is about 1.17, line 8's 7 over 6. H(n)H(n) is more than 1 from 2 numbers on, so the average stays under n−1n - 1, the most swaps by the learn page's ladder on them.

try it on small rows: over every order of 3 numbers, the average EE is about 1.17

this tests where the formula comes from, on the learn page

what a full answer includes
  • a pass swaps unless its smallest is already home, which happens with chance 1 over the numbers left
  • the numbers left after a pass are still a shuffle, with a reason
  • the average swaps n - H(n), about 992.51 for 1,000
  • the comparisons 499,500 for 1,000 records, and the whole cost about 598,751
  • the comparisons grow like n times n and the swaps stay under n, so the comparisons take over on long lists