Sorting Basics
Putting items in order is often the cheapest way to make a hard question easy: neighbours become comparable, duplicates sit together, and searching gets fast.
After this lesson you will be able to
- Explain why sorting is such a useful first step, with at least three examples.
- Trace bubble, selection and insertion sort and say why each is O(n²).
- Use Python's
sorted/sortwithkeyandreverse, and explain stability. - Sort by several criteria at once, using tuples in the key.
- Recognise when sorting turns a problem into a simple scan.
Before you start. Arrays: you should be comfortable with indices, swapping two items, and nested loops.