30.2 Quicksort Algorithm
Last updated
Last updated
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.
The properties that partitioning provides inspires Tony Hoare’s Quicksort algorithm.
Quicksort Algorithm
To quicksort N items:
Partition on the leftmost item as the pivot.
Recursively quicksort the left half.
Recursively quicksort the right half.