Drills
Five tasks.
1. When bubble sort loses its flag
Remove the swapped flag from your BubbleSort and run it with
-order sorted.
How many comparisons now? Relate the number to n. In one sentence, explain why a single boolean changes the best-case complexity class.
2. InsertionSortBinary
Insertion sort finds each item's place linearly, shifting one at a time.
Replace that search with lesson 6's LowerBound.
The comparison count should drop sharply. And the swaps? Measure both, and explain why the total time barely moves.
(This is one of the more important results in the course: you reduced one cost and left the other exactly where it was.)
3. The price of stability
Make SelectionSort stable: instead of swapping, insert the smallest item
into place, shifting the rest along.
How many swaps now? Compare with n−1. Is it still selection sort?
4. Your own crossover
In step 6 the crossover between insertion and sort.Slice on nearly-sorted data
was around 2,000.
Find yours. Measure at 0%, 1% and 5% disorder. How does the crossover move as the disorder grows?
5. When sorting is not worth it at all
You have 1,000,000 items and need the ten best-rated.
Compare two routes: (a) sort everything and take ten; (b) one pass over the list, keeping the best ten as you go.
Count both. The difference should be enormous. Lesson 9 builds the structure that does (b) properly.