30.2 Quicksort Algorithm

The figure below shows a partition performed on the pivot of 5. Notice how 5 in its “proper place” (in other words, it’s exactly where it should be if the entire array was sorted).

This suggests that the partition procedure can be used recursively on the two halves to the left and to the right of the pivot element.

After partitioning on the pivot element of 5, 5 is in the “proper place” and there are two halves left to be sorted. The left half are the four elements of [3, 2, 1, 4], and the right half consists of the three elements of [7, 8, 6].

The properties that partitioning provides inspires Tony Hoare’s Quicksort algorithm.

Quicksort Algorithm

To quicksort N items:

  1. Partition on the leftmost item as the pivot.

  2. Recursively quicksort the left half.

  3. Recursively quicksort the right half.

Last updated