1 of 4

Your RightSignal

Algorithms · 3 of 131 / 3

Quicksort

The concept

How splitting a list around one number ends up sorting all of it

Quicksort picks one element, the pivot, and partitions the list: smaller elements go to its left, larger ones to its right. The pivot is now in its final place, and quicksort recurses on each side.

Take [7, 2, 9, 4, 1, 8, 5] with pivot 5. Partitioning gives [2, 4, 1] 5 [7, 9, 8], which fixes 5 at index 3. Each side is sorted the same way until every piece has one element or none.

Partitioning works in place: it swaps elements within the array, with no second copy. Merge sort does its work after recursing, but quicksort does it before, so there is nothing to merge at the end.