← Course home

Search and ordering · Lesson 20 of 52

Quicksort and partition

Use a partition invariant and relate pivot balance to quicksort's cost.

Full lessonAbout 10 min

You will learn to state a partition invariant and relate pivot splits and duplicates to time and stack cost.

Recommended first: Comparison sorts.

On this page 6 sections

The idea

Partition of [3, 1, 4, 2] with pivot 2 leaves [1, 2, 4, 3] before the recursive calls.

Quicksort picks a pivot and partitions the range. Every value before the pivot is at most the pivot. Every value after it is greater, and then the two sides are sorted. The library's partition swaps the chosen pivot to the end and scans once. boundary is the next slot that should receive a value at most the pivot. For pivot 2 at the end of [3, 1, 4, 2], the scan swaps 1 to index 0. The next swap places 2 at index 1, leaving [1, 2, 4, 3]. The recursive calls sort the two sides. quick_sort on this input returns [1, 2, 3, 4]. The pivot index itself is random unless you pass a Random instance.

A worked trace

StepStateWhat changed
pivotarray = [3, 1, 4, 2]Pivot 2 sits at index 3.
swaparray = [1, 3, 4, 2]Swap index 1 with the boundary at 0.
pivotarray = [1, 2, 4, 3]Pivot 3 sits at index 3.

Why it is correct

The partition invariant is that every index before boundary holds a value ≤ pivot. Every index from boundary to the scan cursor holds a value > pivot. It is true before the scan because that region is empty. Seeing a value > pivot extends the right region without moving boundary. Seeing a value ≤ pivot swaps it into the boundary slot and advances the boundary, which restores the invariant. The final swap puts the pivot at boundary. Everything before it is ≤ pivot and everything after it is > pivot, so the pivot is in its final sorted index.

A common mistake

The two sides are not sorted when the pivot lands. Only the pivot index is final.

What it costs

One partition of k items is Θ(k) time and O(1) extra memory. If every pivot splits off one item, the recurrence is T(n) = T(n - 1) + Θ(n). That shape is Θ(n²), and the call stack is Θ(n) deep. Balanced pivots give T(n) = 2 T(n/2) + Θ(n), which is Θ(n log n) time and Θ(log n) stack. The sort is not stable, because the swaps move equal keys past each other. A three-way partition gathers equals in the middle so a run of duplicate keys does not fall into the n² case.

Run it

python
import randomfrom bitsandbytes.sorting.quick_sort import quick_sortprint(quick_sort([3, 1, 4, 2], rng=random.Random(0)))# prints: [1, 2, 3, 4] 

Practice and next step

Open Binary search next. Binary search uses a sorted list and throws away half of the remaining range.

Implementation and tests Optional reference

Library code 1

  • bitsandbytes.sorting.quick_sort

Use these references to compare the explanation with repository code and tests. A test link means it imports a lesson symbol; it does not by itself claim complete behavior coverage.

Step-by-step trace

Partition [3, 1, 4, 2] with pivot 2

The library swaps a chosen pivot to the end, then scans. This trace starts after 2 is already at the end, which is what happens when the chosen index is the last index.

array
[3, 1, 4, 2]

State

  1. 03taken
  2. 11taken
  3. 24taken
  4. 32taken

Pivot 2 sits at index 3.

Practice feedback is loading. The lesson explanation remains available above.