![]() |
VOOZH | about |
Given an array arr[], partition the array by assuming last element as pivot element.
The partition of an array must satisfy the following two conditions:
Note: There might me more than one possible partition arrays.
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, 16, 19, 9]
Output: [4, 9, 9, 10, 16, 19]
Explanation: All elements smaller than pivot element [4] were arranged before it and elements larger than or equal to pivot [9, 10, 16, 19] were arranged after it.
Table of Content
The Lomuto partition algorithm divides an array based on a pivot element. One pointer marks the boundary for elements smaller than the pivot, while the other pointer helps in array traversal. As we traverse the array, smaller elements are moved to the left of the boundary and boundary expands. After the traversal, all elements to the left of the boundary are smaller, and those on the right are larger than pivot.
Step-by-step explanation of algorithm:
5 6 8 9 12 11 13