Imagine a big pile of things that need matching up, but you can only compare two at a time, and there are strict rules about which comparisons are even allowed.
Checking pairs at random gets painfully slow, fast: ten items take a few dozen checks, but a thousand could take up to a million.
There has to be a smarter way, one that eliminates a big chunk of possibilities with every single comparison. Let's find it, starting with a story π
A mechanic dropped a box of nuts and bolts and now they're all mixed up! Each bolt has exactly one matching nut somewhere in the pile, same size, different piece. Your job: pair every nut with its bolt.
But there's a catch. The mechanic's workbench only lets you test one nut against one bolt at a time.
Every time you test a nut against a bolt, the workbench tells you one of three things:
Sounds simple, but can you find a clever way to pair them all up, without wasting hundreds of comparisons?
Tap a nut, then a bolt (or drag a nut onto a bolt) to test them against each other.
Play manually by tapping/dragging, or turn on Auto Play to watch it happen. Your choice!
Loadingβ¦
You just built it with your own hands. Head to the Key Learnings chapter for the full recap. π οΈ
Sit with these for a moment, there's no single correct wording, just think them through in your own words.
Now that you have built it with your own hands, let's give the idea its proper name and look at the concepts that make it work.
Quick Sort is a sorting algorithm that follows the divide and conquer strategy. It picks one element as a pivot, compares every other element with that pivot, and partitions the list into a group of smaller elements and a group of larger elements. The pivot lands in its final sorted position, and the same process is then repeated on each group until every group has at most one element.
π§ In the workshop: the chosen bolt was the pivot, testing nuts against it was the comparison, the smaller and larger piles were the partitions, and the matched pair locked in the middle was the pivot in its final place.
The reference element chosen to split the list. Every other element is compared only with the pivot, never with each other. Any element can be the pivot, but a pivot near the middle value gives better results.
Each comparison with the pivot answers one question: does this element belong before the pivot or after it? That single answer decides which group the element joins, so no element needs to be compared with every other element.
The core step of Quick Sort. After one pass, smaller elements sit on one side, larger elements sit on the other, and the pivot sits in between. The two sides are not sorted yet, but they never need to be mixed again.
Once partitioning is done, the pivot is exactly where it will be in the sorted list. It is never compared or moved again, which is why the work shrinks with every round.
A big problem is broken into smaller problems of the same kind. Sorting a long list becomes sorting two shorter lists, which is far easier than handling everything at once.
The same steps are applied again on each smaller group. The process stops when a group has zero or one element, because a group that small is already sorted. This stopping point is called the base case.
When the two groups are close in size, the list shrinks quickly and Quick Sort is fast, taking about n Γ log n comparisons on average. When one group is almost empty and the other is almost everything, much more work is left, and in the worst case the effort grows to about n Γ n.
Ranking scores, ordering files by size, arranging products by price, and many matching problems. Anywhere a good reference point can remove a big chunk of the work in one comparison, the Quick Sort idea applies.
New situations to think through using what you just discovered.