0
0
DSA Javascriptprogramming~10 mins

Quick Sort Partition Lomuto and Hoare in DSA Javascript - 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 from low to high-1
If arr[j
i++, swap arr[i
After loop, swap arr[i+1
Return i+1 as pivot index
Choose pivot = arr[low
i = low, j = high + 1
Repeat forever
j-- while arr[j
i++ while arr[i
If i >= j return j
Else swap arr[i
Back to Repeat
Lomuto picks last element as pivot and partitions by moving smaller elements left. Hoare picks first element as pivot and partitions by moving pointers inward and swapping.
Execution Sample
DSA Javascript
function lomutoPartition(arr, low, high) {
  let pivot = arr[high];
  let i = low - 1;
  for (let j = low; j < high; j++) {
    if (arr[j] <= pivot) {
      i++;
      [arr[i], arr[j]] = [arr[j], arr[i]];
    }
  }
  [arr[i + 1], arr[high]] = [arr[high], arr[i + 1]];
  return i + 1;
}
This code partitions the array using Lomuto scheme, placing elements smaller or equal to pivot on left and returns pivot index.
Execution Table
StepOperationijPivotArray StatePointer ChangesVisual State
1Initialize i = low - 1-1-5[3, 8, 2, 5, 1, 4]i = -13 -> 8 -> 2 -> 5 -> 1 -> 4
2j = 0, arr[j] = 3 <= 5?-105[3, 8, 2, 5, 1, 4]No change3 -> 8 -> 2 -> 5 -> 1 -> 4
3arr[j] <= pivot, i++ and swap arr[i], arr[j]005[3, 8, 2, 5, 1, 4]i=0, swap arr[0], arr[0]3 -> 8 -> 2 -> 5 -> 1 -> 4
4j = 1, arr[j] = 8 <= 5?015[3, 8, 2, 5, 1, 4]No change3 -> 8 -> 2 -> 5 -> 1 -> 4
5j = 2, arr[j] = 2 <= 5?025[3, 8, 2, 5, 1, 4]No change3 -> 8 -> 2 -> 5 -> 1 -> 4
6arr[j] <= pivot, i++ and swap arr[i], arr[j]125[3, 2, 8, 5, 1, 4]i=1, swap arr[1], arr[2]3 -> 2 -> 8 -> 5 -> 1 -> 4
7j = 3, arr[j] = 5 <= 5?135[3, 2, 8, 5, 1, 4]No change3 -> 2 -> 8 -> 5 -> 1 -> 4
8arr[j] <= pivot, i++ and swap arr[i], arr[j]235[3, 2, 5, 8, 1, 4]i=2, swap arr[2], arr[3]3 -> 2 -> 5 -> 8 -> 1 -> 4
9j = 4, arr[j] = 1 <= 5?245[3, 2, 5, 8, 1, 4]No change3 -> 2 -> 5 -> 8 -> 1 -> 4
10arr[j] <= pivot, i++ and swap arr[i], arr[j]345[3, 2, 5, 1, 8, 4]i=3, swap arr[3], arr[4]3 -> 2 -> 5 -> 1 -> 8 -> 4
11j = 5, arr[j] = 4 <= 5?355[3, 2, 5, 1, 8, 4]No change3 -> 2 -> 5 -> 1 -> 8 -> 4
12arr[j] <= pivot, i++ and swap arr[i], arr[j]455[3, 2, 5, 1, 4, 8]i=4, swap arr[4], arr[5]3 -> 2 -> 5 -> 1 -> 4 -> 8
13After loop, swap arr[i+1], pivot4-5[3, 2, 5, 1, 4, 8]swap arr[5], arr[5] (pivot stays)3 -> 2 -> 5 -> 1 -> 4 -> 8
14Return pivot index i+1 = 54-5[3, 2, 5, 1, 4, 8]-3 -> 2 -> 5 -> 1 -> 4 -> 8
15Hoare Partition start: i = low = 0, j = high + 1 = 6063[3, 2, 5, 1, 4, 8]i=0, j=63 -> 2 -> 5 -> 1 -> 4 -> 8
16Decrement j while arr[j] > pivot (arr[5]=8>3), j=5->4->3->2023[3, 2, 5, 1, 4, 8]j moves to 23 -> 2 -> 5 -> 1 -> 4 -> 8
17Increment i while arr[i] < pivot (arr[0]=3<3? No), i=0023[3, 2, 5, 1, 4, 8]i stays 03 -> 2 -> 5 -> 1 -> 4 -> 8
18i < j? 0 < 2 yes, swap arr[i], arr[j]023[5, 2, 3, 1, 4, 8]swap arr[0], arr[2]5 -> 2 -> 3 -> 1 -> 4 -> 8
19Decrement j while arr[j] > pivot (arr[2]=3>3? No), j=2023[5, 2, 3, 1, 4, 8]j stays 25 -> 2 -> 3 -> 1 -> 4 -> 8
20Increment i while arr[i] < pivot (arr[1]=2<3 yes), i=1->2223[5, 2, 3, 1, 4, 8]i moves to 25 -> 2 -> 3 -> 1 -> 4 -> 8
21i >= j? 2 >= 2 yes, return j=2223[5, 2, 3, 1, 4, 8]partition index = 25 -> 2 -> 3 -> 1 -> 4 -> 8
💡 Lomuto ends after placing pivot at correct index; Hoare ends when pointers cross (i >= j)
Variable Tracker
VariableStartAfter Step 3After Step 6After Step 8After Step 10After Step 12Final
i (Lomuto)-1012344
j (Lomuto)-02345-
Pivot (Lomuto)5555555
Array (Lomuto)[3,8,2,5,1,4][3,8,2,5,1,4][3,2,8,5,1,4][3,2,5,8,1,4][3,2,5,1,8,4][3,2,5,1,4,8][3,2,5,1,4,8]
i (Hoare)0-----2
j (Hoare)6-----2
Pivot (Hoare)3-----3
Array (Hoare)[3,2,5,1,4,8][5,2,3,1,4,8][5,2,3,1,4,8][5,2,3,1,4,8][5,2,3,1,4,8][5,2,3,1,4,8][5,2,3,1,4,8]
Key Moments - 3 Insights
Why does Lomuto partition use i = low - 1 initially?
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 step 1 and 3 where i increments before swapping.
In Hoare partition, why do we decrement j and increment i before swapping?
We move j left until we find element <= pivot and i right until element >= pivot. This ensures elements less than pivot go left, greater go right. See execution_table steps 16-18 for pointer moves and swap.
Why does Hoare partition return j, not i?
Because j is the last index where left side elements are <= pivot. When i >= j, pointers crossed and partitioning is done. Returning j splits array correctly. See execution_table step 21.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of i after step 6 in Lomuto partition?
A0
B2
C1
D-1
💡 Hint
Check the 'i' column in execution_table row with step 6.
At which step does Hoare partition swap elements for the first time?
AStep 18
BStep 16
CStep 20
DStep 21
💡 Hint
Look for 'swap' in the Operation column for Hoare partition steps.
If in Lomuto partition the pivot was chosen as the first element instead of last, how would the pointer i initialization change?
Ai would start at low - 1
Bi would start at low
Ci would start at high
Di would start at 0
💡 Hint
Recall that i tracks last smaller element index; changing pivot position affects i start.
Concept Snapshot
Quick Sort Partition Schemes:
- Lomuto: pivot = last element
  i starts at low-1
  j scans low to high-1
  swap if arr[j] <= pivot
  finally swap pivot to i+1
- Hoare: pivot = first element
  i = low, j = high+1
  move i right while arr[i]<pivot
  move j left while arr[j]>pivot
  swap arr[i], arr[j] if i<j
  else return j
Both partition arrays around pivot for recursive quicksort.
Full Transcript
This visual execution traces two popular quick sort partition methods: Lomuto and Hoare. Lomuto picks the last element as pivot and uses a pointer i starting before low to track smaller elements. It scans with j and swaps elements smaller or equal to pivot to the left. After scanning, it places the pivot after the last smaller element. Hoare picks the first element as pivot and uses two pointers i and j starting inside and outside the array bounds respectively. It moves i right until it finds an element not less than pivot, and j left until it finds an element not greater than pivot, then swaps them. When pointers cross, partitioning ends. The execution table shows step-by-step pointer movements, swaps, and array states for both methods on the example array [3,8,2,5,1,4]. Variable tracker summarizes pointer and array changes. Key moments clarify common confusions about pointer initialization and return values. Visual quiz tests understanding of pointer values and swap steps. The snapshot summarizes the key steps and differences between Lomuto and Hoare partitions.