Search⌘ K
AI Features

Linear-time Partitioning

Explore how quicksort achieves efficient sorting through linear-time partitioning. Learn to divide subarrays around a pivot, organizing elements less or greater than the pivot. Understand the step-by-step process of partitioning that enables quicksort’s recursive sorting strategy.

We'll cover the following...

The real work of quicksort happens during the divide step, which partitions subarray array[pr]array[p \cdots r] around a pivot drawn from the subarray. Although we can choose any element in the subarray as the pivot, it's easy to implement partitioning if we choose the rightmost element of the subarray, A[r]A[r], as the pivot.

Having chosen a pivot, we partition the subarray by going through it, left to right, comparing each element with the pivot. We maintain two indices q and j into the subarray that divide it up into four groups. We use the variable name q because that index will eventually point at our pivot. We use j because it's a common counter variable name, and the variable will be discarded once we're done.

  • The elements in array[pq1]array[p \cdots q-1] are "group L," consisting of elements known to be less than or equal to the pivot.

  • The elements in ...