Open In App

Partition Algorithms - Complete Tutorial

Last Updated : 11 Nov, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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.

Naive Partition Algorithm

The Naive Partition Algorithm partitions an array by using a temporary array.

  • We first iterates to place elements smaller than or equal to the pivot (the last element) at the start.
  • then places larger elements afterward.
  • Finally, we copy the temporary array back into the original 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.

Lomuto 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

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.

Applications

  1. QuickSort: Both Lomuto’s and Hoare’s partition schemes are central to QuickSort. They split the array around a pivot, recursively sorting the partitions.
    Hoare's method often provides better performance due to fewer swaps.
  2. Kth Smallest/Largest Element: Partition algorithms are efficient for finding the kth smallest or largest element in an array. By partitioning around a pivot, we can reduce the search space to only the required partition, achieving average O(n) time complexity.
  3. Median of Medians: Partitioning is used in the Median of Medians algorithm, which finds an approximate median to use as a pivot. This guarantees worst-case O(n) performance for selection problems.
  4. Sort an array containing two types of elements: Sorting an array with two types of elements, such as positive and negative numbers or even and odd values, can be achieved efficiently using partitioning techniques.
  5. Dutch National Flag Problem: Partitioning is key to solving the Dutch National Flag problem, where the goal is to reorder an array containing three distinct values (e.g., 0s, 1s, and 2s) such that similar values are grouped. A variant of the partitioning algorithm efficiently handles this in linear time with a single pass.



Next Article
Article Tags :
Practice Tags :

Similar Reads