Module 7 of 39

Quicksort: partitioning, pivots, and running time

Partition around a pivot and see why the quality of the split controls quicksort's cost.

Merge sort splits a list by position, sorts both halves, then combines them. Quicksort does the separating work first. It chooses a pivot and rearranges the list so values on one side are no greater than the pivot and values on the other side are no smaller. It then sorts the two sides separately. Once both sides are sorted, the whole list is sorted; there is no merge step.

Background for this page. A partition separates values around a pivot. Recursion sorts smaller groups by applying the same method again. Θ(n) means work proportional to group size. Related explanations: Divide and conquer: merge sort and recurrences · Randomized algorithms and expected running time.

Take [8, 3, 7, 1, 5] and choose 5 as the pivot. One valid partition is [3, 1] | 5 | [8, 7]. The values inside either side need not be sorted yet. The partition has only made a promise about which side they belong on. Sorting [3, 1] and [8, 7] finishes the job.

Partitioning need not sort either side; it places the pivot correctly.

That promise is an invariant of the partition step. A partition implementation moves through the input while maintaining a boundary between examined values that belong on the left and values still to be examined. When it finishes, the pivot is in its final sorted position. This is why recursive calls may ignore the pivot.

The split determines the cost

Partitioning one group of n values costs Θ(n). If each pivot leaves two roughly equal groups, there are about log₂ n levels, giving Θ(n log n) work. If the pivot is always the smallest or largest value, one side is empty and the other has n − 1 values. The recurrence is then roughly T(n) = T(n − 1) + Θ(n), which gives Θ(n²) work.

Choosing a pivot at random makes that bad pattern unlikely to repeat across levels, regardless of the original order. Randomized quicksort has expected Θ(n log n) time, but its worst case remains Θ(n²). “Expected” is a claim over the algorithm’s random choices, not a guarantee about one run. A good implementation also limits recursion depth or changes strategy when splits stay poor.

Here is the main idea without tying it to one of several partition implementations:

# A group with at most one item is already sorted.
if the group has fewer than two items: return
# Put a pivot between values on its two sides.
partition the group around a chosen pivot
# Sort each side; the pivot is already in place.
quicksort the left side
quicksort the right side

Many versions rearrange values within the original array, which can save the extra result array commonly used by merge sort. The recursive calls still use stack space. Ordinary quicksort is not stable: equal-key records may exchange order. The exact memory bound depends on the partition method and how recursion is managed, so “in place” does not mean “uses no extra memory.”

Follow the partition boundary

In [8, 3, 7, 1, 5], choose final value 5 as pivot. Scan the other items, keeping a boundary for values no greater than 5. Move 3 to the left, giving [3, 8, 7, 1, 5], then move 1 beside it, giving [3, 1, 7, 8, 5]. Put 5 after that group: [3, 1, 5, 8, 7]. The boundary invariant explains the partition. Duplicates require care: a two-way partition can send every equal key to one side, giving quadratic time even with random pivots.

Practice. If the pivot is 4, is [2, 3] | 4 | [7, 6] a valid partition? Is the whole list sorted? What happens if every split places the pivot at one end?

Answer. It is a valid partition: every left value is at most 4 and every right value is at least 4. It is not yet sorted because 7 comes before 6. If every split places the pivot at one end, the work can grow quadratically.

Try another case. Does choosing a random pivot fix an all-equal array?

Reasoning. Not by itself. Every pivot value is the same. A three-way partition can group equal keys and avoid sorting them repeatedly.

View the full course and choose another topic.

Image preview

Fit