Codele / Algorithms
Quick sort
To split a class by height, pick one pupil and say: shorter ones stand on the left, taller ones on the right. Then do the same inside each group.
- Quick sort picks a pivot bar, then moves smaller bars to its left and bigger bars to its right. This step is called partitioning.
- After partitioning, the pivot is in its final place. Each side is a smaller problem of the same kind.
- A stack of ranges remembers which sides still need sorting. Pop one, partition it, push its sides.
- Usually it takes about n times log n steps, written O(n log n), and it is very fast in practice.
- Bad luck hurts: if the pivot is always the smallest or biggest bar, one side is empty each time and the work grows to n times n.
Loading the playable lesson…