Partition Algorithms - Complete Tutorial
Last Updated :
11 Nov, 2024
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
- 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. - 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.
- 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.
- 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.
- 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.
Similar Reads
Lomuto Partition Algorithm Given an array arr[], the task is to partition the array by assuming last element as pivot element. The partition of an array must satisfy the following two conditions:Elements smaller than the pivot element appear before pivot in the array.Elements larger than or equal to the pivot element appear a
7 min read
Hoare's Partition Algorithm Given an array arr[], the task is to partition the array by assuming first element as pivot element. The partition of an array must satisfy the following two conditions:Elements smaller than pivot element must appear at index less than or equal to partition index.Elements larger than or equal to piv
7 min read
Naive Partition Algorithm Given an array arr[], the task is to partition the array by assuming last element as pivot element. The partition of an array must satisfy the following two conditions:Elements smaller than or equal to the pivot element appear before pivot in the array.Elements larger than the pivot element appear a
7 min read
Introduction to Divide and Conquer Algorithm Divide and Conquer Algorithm is a problem-solving technique used to solve problems by dividing the main problem into subproblems, solving them individually and then merging them to find solution to the original problem. Divide and Conquer is mainly useful when we divide a problem into independent su
9 min read
Generate all partition of a set Given a set A = {1, 2, 3, . . ., n }. It is called a partition of the set A if the following conditions follow: The union of all the sets is the set AThe intersection of any two sets is an empty setExamples: Input: n = 3Output: [{1, 2, 3}], [{1, 2}, {3}], [{1, 3}, {2}], [{1}, {2, 3}], [{1}, {2}, {3}
8 min read
Maximum Balanced String Partitions Given a balanced string str of size N with an equal number of L and R, the task is to find a maximum number X, such that a given string can be partitioned into X balanced substring. A string called to be balanced if the number of 'L's in the string equals the number of 'R's. Examples: Input : str =
7 min read
Bell Numbers (Number of ways to Partition a Set) Given a set of n elements, find the number of ways of partitioning it. Examples: Input: n = 2Output: 2Explanation: Let the set be {1, 2}. The partitions are {{1},{2}} and {{1, 2}}.Input: n = 3Output: 5Explanation: Let the set be {1, 2, 3}. The partitions are {{1},{2},{3}}, {{1},{2, 3}}, {{2},{1, 3}}
15+ min read
Generate all unique partitions of an integer Given a positive integer n, generate all possible unique ways to represent n as sum of positive integers. Examples: Input: n = 2 Output: 2 1 1 Input: n = 3 Output: 3 2 1 1 1 1 Note: 2+1 and 1+2 are considered as duplicates. Input: n = 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1 Recommended ProblemUnique parti
15+ min read
Three way partitioning around an element Given an array arr[] of integers and a value pivot, the task is to partition the array around the pivot such that array is divided in three parts. All elements smaller than pivot come first. All elements equal to pivot come next. All elements greater than pivot appear in the end. The individual elem
14 min read
Generate all unique partitions of an integer | Set 2 Given a positive integer n, the task is to generate all possible unique ways to represent n as sum of positive integers.Examples: Input: 4 Output: 4 3 1 2 2 2 1 1 1 1 1 1Input: 3 Output: 3 2 1 1 1 1 Approach: We have already discussed the implementation of generating unique partitions in this post.
8 min read