0
0
DSA C++programming

Quick Sort Partition Lomuto and Hoare in DSA C++

Choose your learning style9 modes available
Mental Model
Partition rearranges elements around a pivot so smaller go left and bigger go right. Lomuto uses one pointer scanning forward, Hoare uses two pointers scanning inward.
Analogy: Imagine sorting books on a shelf by height. Lomuto picks a book and moves through the shelf placing shorter books to the left and taller to the right. Hoare uses two people starting at each end swapping books that are out of place until they meet in the middle.
Array: [5, 3, 8, 4, 2]
Pivot chosen (Lomuto): 2 (last element)
Pointers:
Lomuto: i and j scanning right
Hoare: i from left, j from right

Initial:
[5, 3, 8, 4, 2]
 i,j -> start

After partition:
[2, 3, 8, 4, 5]
Pivot at correct place
Dry Run Walkthrough
Input: array: [5, 3, 8, 4, 2]
Goal: Partition the array around a pivot using Lomuto and Hoare methods
Step 1: Lomuto: choose pivot=2 (last element), set i=-1
[5, 3, 8, 4, 2]
i=-1, j=0
Why: Pivot chosen at end; i tracks smaller elements boundary
Step 2: Lomuto: j=0, 5 > 2 no swap, i stays -1
[5, 3, 8, 4, 2]
i=-1, j=0
Why: 5 is bigger than pivot, no change
Step 3: Lomuto: j=1, 3 > 2 no swap, i stays -1
[5, 3, 8, 4, 2]
i=-1, j=1
Why: 3 is bigger than pivot, no change
Step 4: Lomuto: j=2, 8 > 2 no swap, i stays -1
[5, 3, 8, 4, 2]
i=-1, j=2
Why: 8 is bigger than pivot, no change
Step 5: Lomuto: j=3, 4 > 2 no swap, i stays -1
[5, 3, 8, 4, 2]
i=-1, j=3
Why: 4 is bigger than pivot, no change
Step 6: Lomuto: swap pivot with element at i+1 (index 0)
[2, 3, 8, 4, 5]
i=0, pivot index=0
Why: Place pivot in correct position
Step 7: Hoare: choose pivot=5 (first element), i=0, j=4
[5, 3, 8, 4, 2]
i=0, j=4
Why: Pivot chosen at start; i and j scan inward
Step 8: Hoare: i moves right while arr[i]<pivot: i=1 (3<5), i=2 (8>5 stop)
[5, 3, 8, 4, 2]
i=2, j=4
Why: Find element on left bigger than pivot
Step 9: Hoare: j moves left while arr[j]>pivot: j=4 (2<5 stop)
[5, 3, 8, 4, 2]
i=2, j=4
Why: Find element on right smaller than pivot
Step 10: Hoare: swap arr[i] and arr[j]: swap 8 and 2
[5, 3, 2, 4, 8]
i=2, j=4
Why: Put smaller element left, bigger right
Step 11: Hoare: i moves right: i=3 (4<5), i=4 (8>5 stop)
[5, 3, 2, 4, 8]
i=4, j=4
Why: Find next bigger element on left
Step 12: Hoare: j moves left: j=3 (4<5 stop)
[5, 3, 2, 4, 8]
i=4, j=3
Why: Find next smaller element on right
Step 13: Hoare: i >= j stop, partition index = j=3
[5, 3, 2, 4, 8]
i=4, j=3
Why: Pointers crossed, partition done
Result:
Lomuto partitioned array: [2, 3, 8, 4, 5]
Pivot index: 0
Hoare partitioned array: [5, 3, 2, 4, 8]
Partition index: 3
Annotated Code
DSA C++
#include <iostream>
#include <vector>
using namespace std;

// Lomuto partition scheme
int lomuto_partition(vector<int>& arr, int low, int high) {
    int pivot = arr[high];
    int i = low - 1; // boundary for smaller elements
    for (int j = low; j < high; j++) {
        if (arr[j] <= pivot) {
            i++;
            swap(arr[i], arr[j]);
        }
    }
    swap(arr[i + 1], arr[high]);
    return i + 1; // pivot index
}

// Hoare partition scheme
int hoare_partition(vector<int>& arr, int low, int high) {
    int pivot = arr[low];
    int i = low - 1;
    int j = high + 1;
    while (true) {
        do {
            i++;
        } while (arr[i] < pivot);
        do {
            j--;
        } while (arr[j] > pivot);
        if (i >= j) return j;
        swap(arr[i], arr[j]);
    }
}

void print_array(const vector<int>& arr) {
    for (int v : arr) cout << v << " ";
    cout << endl;
}

int main() {
    vector<int> arr1 = {5, 3, 8, 4, 2};
    vector<int> arr2 = arr1; // copy for hoare

    int lomuto_pivot = lomuto_partition(arr1, 0, arr1.size() - 1);
    cout << "Lomuto partitioned array: ";
    print_array(arr1);
    cout << "Pivot index: " << lomuto_pivot << endl;

    int hoare_pivot = hoare_partition(arr2, 0, arr2.size() - 1);
    cout << "Hoare partitioned array: ";
    print_array(arr2);
    cout << "Partition index: " << hoare_pivot << endl;

    return 0;
}
int pivot = arr[high];
select pivot as last element for Lomuto
int i = low - 1;
initialize boundary for smaller elements
for (int j = low; j < high; j++) { if (arr[j] <= pivot) { i++; swap(arr[i], arr[j]); } }
scan and swap elements smaller or equal to pivot to left
swap(arr[i + 1], arr[high]);
place pivot after smaller elements
int pivot = arr[low];
select pivot as first element for Hoare
int i = low - 1; int j = high + 1;
initialize two pointers outside array bounds
do { i++; } while (arr[i] < pivot);
move i right until element >= pivot
do { j--; } while (arr[j] > pivot);
move j left until element <= pivot
if (i >= j) return j;
stop when pointers cross
swap(arr[i], arr[j]);
swap out-of-place elements
OutputSuccess
Lomuto partitioned array: 2 3 8 4 5 Pivot index: 0 Hoare partitioned array: 5 3 2 4 8 Partition index: 3
Complexity Analysis
Time: O(n) because partition scans the array once comparing and swapping elements
Space: O(1) because partitioning is done in-place without extra arrays
vs Alternative: Both Lomuto and Hoare partition run in linear time, but Hoare often does fewer swaps and can be faster in practice
Edge Cases
empty array
partition returns immediately without changes
DSA C++
for (int j = low; j < high; j++) { ... } in lomuto_partition
array with one element
partition returns the single index as pivot position
DSA C++
int pivot = arr[high]; in lomuto_partition
all elements equal
Lomuto places pivot at end; Hoare pointers cross quickly with minimal swaps
DSA C++
if (arr[j] <= pivot) in lomuto_partition and do-while loops in hoare_partition
When to Use This Pattern
When you see a problem asking to sort or rearrange elements efficiently, recognize the partition step of quicksort and choose Lomuto or Hoare partition schemes to split the array around a pivot.
Common Mistakes
Mistake: Using Lomuto partition but forgetting to swap the pivot at the end
Fix: Always swap the pivot with element at i+1 after scanning to place it correctly
Mistake: In Hoare partition, returning i instead of j when pointers cross
Fix: Return j because it marks the correct partition boundary
Summary
Partitions an array around a pivot so smaller elements go left and larger go right.
Use when implementing quicksort to efficiently divide the array for recursive sorting.
Lomuto uses one pointer scanning forward; Hoare uses two pointers scanning inward and often swaps fewer times.