Physics I / Searching and sorting ideas
Practice question · Put in order

Trace selection sort on the list [7, 2, 9, 1]. Order the states the list passes through.

Hints
  1. Each pass finds the minimum of the unsorted tail and swaps it to the boundary.
  2. Pass 2 examines [2, 9, 7]: its minimum is already at the front of the tail.
Show the answer
  1. [7, 2, 9, 1], initial list
  2. [1, 2, 9, 7], smallest (1) swapped to front
  3. [1, 2, 9, 7], 2 already in place, no change
  4. [1, 2, 7, 9], 7 swapped into third place
Why

Selection sort grows a sorted prefix one guaranteed-correct element per pass, simple to reason about, but each pass rescans the whole tail: O(n2)O(n^{2}) total. Understanding why it is slow is the first step toward appreciating faster sorts.

Read the lesson: Searching and sorting ideas →

Practise Searching and sorting ideas

The app has 8 more questions on this lesson, and keeps your place in the course. Physics I is free to start.

More questions on Searching and sorting ideas