0
0
DSA C++programming~10 mins

Quick Sort Partition Lomuto and Hoare in DSA C++ - Execution Trace

Choose your learning style9 modes available
Concept Flow - Quick Sort Partition Lomuto and Hoare
Choose pivot = last element
Initialize i = low-1
For j = low to high-1
If arr[j
Increment i
Swap arr[i
After loop, swap arr[i+1
Return i+1 as pivot index
Choose pivot = arr[low
Initialize i = low-1, j = high+1
Repeat forever
Increment i until arr[i
Decrement j until arr[j
If i >= j, return j
Else swap arr[i
Repeat
Lomuto picks last element as pivot and partitions by swapping smaller elements forward. Hoare picks first element as pivot and uses two pointers moving inward to swap out-of-place elements.
Execution Sample
DSA C++
int lomutoPartition(int arr[], int low, int high) {
  int pivot = arr[high];
  int i = low - 1;
  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;
}
This code partitions the array using Lomuto scheme by moving elements smaller than pivot to the front and placing pivot in correct position.
Execution Table
StepOperationijPivotArray StatePointer Changes
1Initialize i = low-1-1-5[3, 8, 2, 5, 1, 4]i = -1
2j=0, arr[j]=3 <= pivot(5)?-105[3, 8, 2, 5, 1, 4]Yes, i=0; swap arr[0], arr[0] (no change)
3j=1, arr[j]=8 <= pivot(5)?015[3, 8, 2, 5, 1, 4]No, i=0
4j=2, arr[j]=2 <= pivot(5)?025[3, 8, 2, 5, 1, 4]Yes, i=1; swap arr[1], arr[2] -> [3, 2, 8, 5, 1, 4]
5j=3, arr[j]=5 <= pivot(5)?135[3, 2, 8, 5, 1, 4]Yes, i=2; swap arr[2], arr[3] -> [3, 2, 5, 8, 1, 4]
6j=4, arr[j]=1 <= pivot(5)?245[3, 2, 5, 8, 1, 4]Yes, i=3; swap arr[3], arr[4] -> [3, 2, 5, 1, 8, 4]
7End loop, swap arr[i+1], arr[high]3-5[3, 2, 5, 1, 4, 8]Swap arr[4], arr[5]
8Return pivot index i+14-5[3, 2, 5, 1, 4, 8]Pivot index = 4
9Hoare init i=low-1, j=high+1-163[3, 2, 5, 1, 4, 8]i=-1, j=6
10Increment i until arr[i]>=pivot063[3, 2, 5, 1, 4, 8]i=0 (arr[0]=3 >=3)
11Decrement j until arr[j]<=pivot053[3, 2, 5, 1, 4, 8]j=5 (arr[5]=8 >3), j=4 (arr[4]=4 >3), j=3 (arr[3]=1 <=3)
12i < j? 0 < 3033[3, 2, 5, 1, 4, 8]Yes, swap arr[0], arr[3] -> [1, 2, 5, 3, 4, 8]
13Increment i until arr[i]>=pivot133[1, 2, 5, 3, 4, 8]i=1 (arr[1]=2 <3), i=2 (arr[2]=5 >=3)
14Decrement j until arr[j]<=pivot223[1, 2, 5, 3, 4, 8]j=2 (arr[2]=5 >3), j=1 (arr[1]=2 <=3)
15i < j? 2 < 1213[1, 2, 5, 3, 4, 8]No, return j=1
16Partition done, pivot index = 1213[1, 2, 5, 3, 4, 8]Hoare partition index = 1
💡 Lomuto ends after placing pivot at correct position; Hoare ends when i >= j and returns partition index.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7After Step 12After Step 15Final
i (Lomuto)-10133---
j (Lomuto)-024----
Pivot (Lomuto)55555---
Array (Lomuto)[3,8,2,5,1,4][3,8,2,5,1,4][3,2,8,5,1,4][3,2,5,1,8,4][3,2,5,1,4,8]---
i (Hoare)-1----022
j (Hoare)6----311
Pivot (Hoare)3----333
Array (Hoare)[3,2,5,1,4,8]----[1,2,5,3,4,8][1,2,5,3,4,8][1,2,5,3,4,8]
Key Moments - 3 Insights
Why does Lomuto partition use i = low - 1 at start?
Because i tracks the last position where a smaller or equal element was placed. Starting at low-1 means no elements placed yet. See execution_table rows 1-2 where i increments before swapping.
Why does Hoare partition return j instead of i?
Because j marks the boundary where elements to the left are <= pivot and to the right are >= pivot. When i >= j, partitioning is done. See execution_table row 15 where i=2, j=1 and j is returned.
Why does Lomuto swap pivot at the end, but Hoare swaps during partition?
Lomuto places pivot last after moving smaller elements forward. Hoare swaps elements out of place during scanning to group smaller and larger elements around pivot. See execution_table rows 7 (Lomuto swap pivot) and 12 (Hoare swap arr[i], arr[j]).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after Lomuto step 6?
A[3, 2, 5, 1, 8, 4]
B[3, 2, 5, 1, 4, 8]
C[3, 8, 2, 5, 1, 4]
D[1, 2, 5, 3, 4, 8]
💡 Hint
Check execution_table row 6 under 'Array State' column.
At which step does Hoare partition swap elements for the first time?
AStep 10
BStep 12
CStep 14
DStep 15
💡 Hint
Look at execution_table row 12 describing swap operation.
If pivot in Lomuto partition was chosen as arr[low] instead of arr[high], how would the initial i value change?
Ai would start at low
Bi would start at low - 1
Ci would start at high
Di would start at 0
💡 Hint
Recall Lomuto starts i at low-1 because pivot is last element; changing pivot to first means i starts at low.
Concept Snapshot
Quick Sort Partition Schemes:
- Lomuto: pivot = last element
- i starts at low-1, j scans low to high-1
- Swap arr[i+1] with pivot at end
- Hoare: pivot = first element
- i = low-1, j = high+1
- Move i right until >= pivot, j left until <= pivot
- Swap arr[i], arr[j] until i >= j
- Lomuto returns i+1, Hoare returns j
- Both partition array around pivot for recursive quicksort
Full Transcript
This visualization shows two popular quick sort partition methods: Lomuto and Hoare. Lomuto chooses the last element as pivot and uses a pointer i to track the boundary of smaller elements. It scans the array with j, swapping elements smaller or equal to pivot forward. After scanning, it swaps the pivot into its correct position and returns the pivot index. Hoare chooses the first element as pivot and uses two pointers i and j starting outside the array bounds. i moves right until it finds an element not less than pivot, j moves left until it finds an element not greater than pivot. If i is less than j, they swap those elements. When i meets or passes j, partitioning ends and j is returned as the partition index. The execution table traces each step showing pointer movements, swaps, and array states. Variable tracker shows how i, j, pivot, and array change over time. Key moments clarify common confusions about pointer initialization and return values. The visual quiz tests understanding of array states and pointer actions during partitioning. The snapshot summarizes the key differences and steps of Lomuto and Hoare partition schemes.