VOOZH about

URL: https://www.geeksforgeeks.org/dsa/lomuto-partition-algorithm/

⇱ Lomuto Partition Algorithm - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Lomuto Partition Algorithm

Last Updated : 19 Mar, 2026

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:

  • 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: 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 O(n) Time and O(1) Space

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.
  • Initialize pointer i at the start; it represents the boundary of elements strictly smaller than the pivot.
  • Traverse the array using pointer j from start to second last element:
  • If arr[j] < pivot, swap arr[i] and arr[j], then increment i.
  • After traversal, all elements smaller than the pivot are on the left side of index i.
  • Swap arr[i] with the pivot to place the pivot at its correct position.
  • Final state: Left of pivot → elements < pivot, Right of pivot → elements ≥ pivot
  • Note: Elements equal to the pivot may appear on the right side, which contributes to QuickSort being unstable.

Output
5 6 8 9 12 11 13 

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.
Comment
Article Tags: