A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/dsa/lomuto-partition-algorithm/ below:

Lomuto Partition Algorithm - GeeksforGeeks

Lomuto Partition Algorithm

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:

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.

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:

C++
// C++ program to partition the array
// using Lomuto Partition Algorithm
#include <iostream>
#include <vector>
using namespace std;

// Function to partition the array according 
// to pivot index element
void partition(vector<int> &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<int> 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4