Sorting: the invariant you are buying
Nobody will ask you to implement quicksort, and everybody will judge the comparator you write, the stability you assume, and whether you noticed that the O(n log n) you just added is now the dominant term — so this lesson treats sorting as a purchase: what invariant does the order give you, what does it cost, and when is the price wrong.
What you'll be able to do
- Write a correct multi-key comparator, and explain what a comparator must guarantee for a sort to be defined at all
- Say what stability means, and use it to build a multi-key sort out of two single-key sorts
- Explain the Omega(n log n) comparison lower bound from the decision-tree argument, and why counting and radix sort are not exceptions to it
- Name the invariant a sort buys in five standard problems, and solve them in one pass afterwards
- Recognise the three cases where sorting is the wrong tool, and say what replaces it
Before this: complexity-honestly, two-pointers
The rest of this lesson is in the app
Nobody will ask you to implement quicksort, and everybody will judge the comparator you write, the stability you assume, and whether you noticed that the O(n log n) you just added is now the dominant term — so this lesson treats sorting as a purchase: what invariant does the order give you, what does it cost, and when is the price wrong. This walkthrough runs about 26 minutes, with runnable code you can edit and re-run as you read.
Continue in ChannelPulseThe first module of every track is free to read on the web — see what's open in Coding & data structures.