Lomuto Partition Algorithm (original) (raw)
Last Updated : 15 Nov, 2024
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 after pivot it in the array.
**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: Allelements 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: Allelements 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
Lomuto Algorithm for Array Partition
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:
- Choose the last element of the array as the **pivot.
- Initialise pointer **
i
**at the start of the array; this pointer will act as the boundary of elements that are smaller than or equal to the **pivot. - Traverse the array with pointer
j
, checking each elementarr[j]
:- If
arr[j]
<= pivot, swaparr[i]
witharr[j]
to move the smaller element to the left and incrementi
** to adjust the boundary.
- If
- After traversing, elements smaller than or equal to the pivot will be at the start of the array, with**
i
**marking the boundary. - Finally, swap
arr[i]
with the **pivot (last element) to place pivot in its correct position. C++ `
// C++ program to partition the array // using Lomuto Partition Algorithm #include #include using namespace std;
// Function to partition the array according // to pivot index element void partition(vector &arr) { int n = arr.size(); int pivot = arr[n - 1];
// i acts as boundary between smaller and
// larger element compared to pivot
int i = -1;
for (int j = 0; j < n; j++) {
// If smaller element is found expand the
// boundary and swapping it with boundary element.
if (arr[j] < pivot) {
i++;
swap(arr[i], arr[j]);
}
}
// place the pivot at its correct position
swap(arr[i + 1], arr[n - 1]);
}
int main() { vector arr = {5, 13, 6, 9, 12, 11, 8}; partition(arr);
for (int i = 0; i < arr.size(); i++)
cout << arr[i] << " ";
return 0;
}
C
// C program to partition the array // using Lomuto Partition Algorithm #include <stdio.h> #include <stdlib.h>
// Function to partition the array according // to pivot index element void partition(int arr[], int n) { int pivot = arr[n - 1];
// i acts as boundary between smaller and
// larger element compared to pivot
int i = -1;
for (int j = 0; j < n; j++) {
// If smaller element is found expand the
// boundary and swapping it with boundary element.
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// place the pivot at its correct position
int temp = arr[i + 1];
arr[i + 1] = arr[n - 1];
arr[n - 1] = temp;
}
int main() { int arr[] = {5, 13, 6, 9, 12, 11, 8}; int n = sizeof(arr) / sizeof(arr[0]); partition(arr, n);
for (int i = 0; i < n; i++)
printf("%d ", arr[i]);
return 0;
}
Java
// Java program to partition the array // using Lomuto Partition Algorithm import java.util.Arrays;
class GfG {
// Function to partition the array according
// to pivot index element
static void partition(int[] arr) {
int n = arr.length;
int pivot = arr[n - 1];
// i acts as boundary between smaller and
// larger element compared to pivot
int i = -1;
for (int j = 0; j < n; j++) {
// If smaller element is found expand the
// boundary and swapping it with boundary element.
if (arr[j] < pivot) {
i++;
int temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// place the pivot at its correct position
int temp = arr[i + 1];
arr[i + 1] = arr[n - 1];
arr[n - 1] = temp;
}
public static void main(String[] args) {
int[] arr = {5, 13, 6, 9, 12, 11, 8};
partition(arr);
for (int ele: arr)
System.out.print(ele + " ");
}
}
Python
Python program to partition the array
using Lomuto Partition Algorithm
def partition(arr): n = len(arr) pivot = arr[n - 1]
# i acts as boundary between smaller and
# larger element compared to pivot
i = -1
for j in range(n):
# If smaller element is found expand the
# boundary and swapping it with boundary element.
if arr[j] < pivot:
i += 1
arr[i], arr[j] = arr[j], arr[i]
# place the pivot at its correct position
arr[i + 1], arr[n - 1] = arr[n - 1], arr[i + 1]
if name == "main": arr = [5, 13, 6, 9, 12, 11, 8] partition(arr) for ele in arr: print(ele, end = ' ')
C#
// C# program to partition the array // using Lomuto Partition Algorithm using System;
class GfG {
// Function to partition the array according
// to pivot index element
static void partition(int[] arr) {
int n = arr.Length, temp;
int pivot = arr[n - 1];
// i acts as boundary between smaller and
// larger element compared to pivot
int i = -1;
for (int j = 0; j < n; j++) {
// If smaller element is found expand the
// boundary and swapping it with boundary element.
if (arr[j] < pivot) {
i++;
temp = arr[i];
arr[i] = arr[j];
arr[j] = temp;
}
}
// place the pivot at its correct position
temp = arr[i+1];
arr[i+1] = arr[n-1];
arr[n-1] = temp;
}
static void Main() {
int[] arr = {5, 13, 6, 9, 12, 11, 8};
partition(arr);
Console.WriteLine(string.Join(" ", arr));
}
}
JavaScript
// JavaScript program to partition the array // using Lomuto Partition Algorithm
// Function to partition the array according // to pivot index element function partition(arr) { let n = arr.length; let pivot = arr[n - 1];
// i acts as boundary between smaller and
// larger element compared to pivot
let i = -1;
for (let j = 0; j < n; j++) {
// If smaller element is found expand the
// boundary and swapping it with boundary element.
if (arr[j] < pivot) {
i++;
[arr[i], arr[j]] = [arr[j], arr[i]];
}
}
// place the pivot at its correct position
[arr[i + 1], arr[n - 1]] = [arr[n - 1], arr[i + 1]];
}
// Driver Code let arr = [5, 13, 6, 9, 12, 11, 8]; partition(arr); console.log(arr.join(" "));
`
**Time Complexity: O(n), for array traversal.
**Auxiliary Space: O(1)
Some Interesting Facts
- We can easily modify the algorithm to consider the first element (or any other element) as pivot by swapping first and last elements and then using the same code.
- It is easier to understand and implement compared to other partitioning algorithms.
- It generally performs slower than Hoare's partition algorithm, especially in cases of large datasets or when the pivot is poorly chosen, as it may require more swaps.
- Despite its inefficiency, it is still widely used in educational books like _Introduction to Algorithms book by Cormen for quick sort, due to its simplicity.