VOOZH about

URL: https://www.geeksforgeeks.org/dsa/hoares-vs-lomuto-partition-scheme-quicksort/

⇱ Hoare's vs Lomuto partition scheme in QuickSort - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Hoare's vs Lomuto partition scheme in QuickSort

Last Updated : 23 Jul, 2025

We have discussed the implementation of QuickSort using Lomuto partition scheme. Lomuto's partition scheme is easy to implement as compared to Hoare scheme. This has inferior performance to Hoare's QuickSort.

Lomuto's Partition Scheme:

This algorithm works by assuming the pivot element as the last element. If any other element is given as a pivot element then swap it first with the last element. Now initialize two variables i as low and j also low,  iterate over the array and increment i when arr[j] <= pivot and swap arr[i] with arr[j] otherwise increment only j. After coming out from the loop swap arr[i+1] with arr[hi]. This i stores the pivot element.

partition(arr[], lo, hi) 
pivot = arr[hi]
i = lo-1 // place for swapping
for j := lo to hi – 1 do
if arr[j] <= pivot then
i = i + 1
swap arr[i] with arr[j]
swap arr[i+1] with arr[hi]
return i+1

Refer QuickSort for details of this partitioning scheme. 
Below are implementations of this approach:-


Output
Sorted array: 
1 5 7 8 9 10 

Time Complexity: O(N2
Auxiliary Space: O(1) 


Hoare's Partition Scheme:

works by initializing two indexes that start at two ends, the two indexes move toward each other until an inversion is (A smaller value on the left side and greater value on the right side) found. When an inversion is found, two values are swapped and the process is repeated.

Algorithm:

partition(arr[], lo, hi)
pivot = arr[lo]
i = lo - 1 // Initialize left index
j = hi + 1 // Initialize right index

// Find a value in left side greater
// than pivot
do
i = i + 1
while arr[i] < pivot

// Find a value in right side smaller
// than pivot
do
j--;
while (arr[j] > pivot);

if i >= j then
return j

swap arr[i] with arr[j]

Below are implementations of this approach:- 


Output
Sorted array: 
1 5 7 8 9 10 

Time Complexity: O(N) 
Auxiliary Space: O(1)

Note : If we change Hoare's partition to pick the last element as pivot, then the Hoare's partition may cause QuickSort to go into an infinite recursion. For example, {10, 5, 6, 20} and pivot is arr[high], then returned index will always be high and call to same QuickSort will be made. To handle a random pivot, we can always swap that random element with the first element and simply follow the above algorithm.
Comparison:

  1. Hoare's scheme is more efficient than Lomuto's partition scheme because it does three times fewer swaps on average, and it creates efficient partitions even when all values are equal.
  2. Like Lomuto's partition scheme, Hoare partitioning also causes Quick sort to degrade to O(n^2) when the input array is already sorted, it also doesn't produce a stable sort.
  3. Note that in this scheme, the pivot's final location is not necessarily at the index that was returned, and the next two segments that the main algorithm recurs on are (lo..p) and (p+1..hi) as opposed to (lo..p-1) and (p+1..hi) as in Lomuto's scheme.
  4. Both Hoare's Partition, as well as Lomuto's partition, are unstable.
Hoare partition algorithmLomuto partition algorithm
Generally, the first item or the element is assumed to be the initial pivot element. Some choose the middle element and even the last element.Generally, a random element of the array is located and picked and then exchanged with the first or the last element to give initial pivot values. In the aforementioned algorithm, the last element of the list is considered as the initial pivot element.
It is a linear algorithm.It is also a linear algorithm.
It is relatively faster.It is slower.
It is slightly difficult to understand and to implement.It is easy to understand and easy to implement.
It doesn't fix the pivot element in the correct position.It fixes the pivot element in the correct position.

Source : https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme

Comment
Article Tags: