![]() |
VOOZH | about |
Partition algorithms are key techniques in computer science, widely used in sorting (like QuickSort) and selection problems. By dividing an array around a pivot, they allow data to be organized into segments for faster sorting and searching.
This tutorial covers popular partitioning methods, including Hoare’s and Lomuto’s, each with unique strategies for arranging elements around a pivot.
Examples:
Input: arr[] = [5, 13, 6, 9, 12, 11, 8]
Output: [5, 6, 8, 13, 9, 12, 11]
Explanation: All elements smaller than pivot element [5, 6] were arranged before it and elements larger than pivot [13, 9, 12, 11] were arranged after it.Input: arr[] = [4, 10, 9, 8, 16, 19, 9]
Output: [4, 9, 8, 9, 10, 16, 19]
Explanation: All elements smaller than or equal to pivot element [4, 9, 8] were arranged before it and elements larger than pivot [10, 16, 19] were arranged after it.
Table of Content
The Naive Partition Algorithm partitions an array by using a temporary array.
While straightforward, this approach is inefficient due to extra space usage and multiple traversals. The pivot is always the last element. This is the only stable partition algorithm among the three.
To know more about the implementation, please refer Naive Partition Algorithm.
The Lomuto Partition Algorithm uses two pointers: one to mark the boundary of elements smaller than or equal to the pivot (the last element) and another to scan the array. Smaller elements are swapped to the left, and after scanning, the pivot is positioned correctly.
Although It is simple to implement, Lomuto's approach can be inefficient on large arrays due to frequent swaps, with the last element always serving as the pivot.
To know more about the implementation, please refer Lomuto Partition Algorithm.
Hoare's Partition Algorithm efficiently partitions an array with two pointers starting from opposite ends, using the first element as the pivot. The pointers move toward each other, swapping elements to keep smaller values on the left and larger ones on the right. This approach reduces swaps and comparisons, making it generally faster than Lomuto’s method. Unlike other algorithms, it uses the first element as the pivot.
To know more about the implementation, please refer Hoare's Partition Algorithm.