Practice question · Put in order
Trace selection sort on the list [7, 2, 9, 1]. Order the states the list passes through.
- [1, 2, 9, 7], 2 already in place, no change
- [7, 2, 9, 1], initial list
- [1, 2, 9, 7], smallest (1) swapped to front
- [1, 2, 7, 9], 7 swapped into third place
Hints
- Each pass finds the minimum of the unsorted tail and swaps it to the boundary.
- Pass 2 examines [2, 9, 7]: its minimum is already at the front of the tail.
Show the answer
- [7, 2, 9, 1], initial list
- [1, 2, 9, 7], smallest (1) swapped to front
- [1, 2, 9, 7], 2 already in place, no change
- [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: total. Understanding why it is slow is the first step toward appreciating faster sorts.
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
- Classify each sort as O(n²) average or O(n log n) average.
- You guess a number between 1 and 1000; after each guess you learn "higher" or "lower". A smart player needs…
- Binary search is far faster than linear search but is used less often in practice. Why is it used less often…
- Order the steps of merge sort on [3, 1, 4, 2].
- Binary search finds a name among 1,000 sorted entries in ~10 steps. Estimate the steps needed for…