tap for sound

Quick Sort Visualization

Quick Sort picks a pivot, splits the list into smaller and larger halves, and sorts each half the same way.

How it works

Quick Sort is a divide-and-conquer algorithm. It picks a pivot value, then walks inward from both ends of the range, swapping pairs that sit on the wrong side, until every value left of the split is smaller than every value right of it. It then repeats the same process on each side, so the problem keeps halving.

On random data that gives about n log n comparisons, and because it swaps in place and touches memory in long runs, it is one of the fastest general-purpose sorts in practice. Its weakness is a bad pivot: if every pivot is the smallest or largest value, the splits are lopsided and the work grows to n². This version uses the middle element (Hoare partition), which handles already-sorted input well.

What to watch for

Watch the colours separate into warm and cool blocks first, then each block splits again, faster and faster, until the rainbow snaps into place.

Complexity

Best
n log n
Average
n log n
Worst
n²
Extra space
log n
Stable
No

Race it

Same shuffle, same speed, one winner: Quick vs Merge. Or open the studio to try other layouts, sizes and speeds, and make a video of it.

FAQ

Is Quick Sort stable?

No. Partitioning swaps values across long distances, so equal values can change order.

Why is Quick Sort usually faster than Merge Sort?

Both do about n log n work on average, but Quick Sort works in place with tight, cache-friendly loops, while Merge Sort copies into extra memory.

When is Quick Sort slow?

When pivots split the data very unevenly, for example a first-element pivot on sorted input. Then it degrades to n² comparisons.