Bird
Raised Fist0
DSA Goprogramming~10 mins

Quick Sort Partition Lomuto and Hoare in DSA Go - Execution Trace

Choose your learning style10 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
↓
i++, swap arr[i
↓
After loop, swap arr[i+1
↓
Return i+1 as pivot index
↓
Choose pivot = arr[low
↓
i = low - 1, j = high + 1
↓
Repeat forever
↓
i++ until arr[i
↓
If i >= j
↓
Return j as pivot index
↓
Else swap arr[i
Shows step-by-step how Lomuto and Hoare partition methods rearrange array elements around a pivot for quicksort.
Execution Sample
DSA Go
func lomutoPartition(arr []int, low, high int) int {
    pivot := arr[high]
    i := low - 1
    for 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
}
Lomuto partition rearranges elements so those <= pivot come before it, returning pivot's final index.
Execution Table
StepOperationijPivotArray StatePointer ChangesAction
1Initialize i = low - 1-1-5[3, 8, 2, 5, 1, 4]i = -1Start loop j from 0
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 stays 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, 8, 4]Swap arr[4], arr[5]Swap arr[4], arr[5] → [3, 2, 5, 1, 4, 8]
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=6Start loop
10i++ until arr[i]>=pivot063[3, 2, 5, 1, 4, 8]i=0arr[0]=3 >=3 stop
11j-- until arr[j]<=pivot053[3, 2, 5, 1, 4, 8]j=5arr[5]=8 >3 continue j=4
12j-- until arr[j]<=pivot043[3, 2, 5, 1, 4, 8]j=4arr[4]=4 >3 continue j=3
13j-- until arr[j]<=pivot033[3, 2, 5, 1, 4, 8]j=3arr[3]=1 <=3 stop
14i < j? i=0, j=3033[3, 2, 5, 1, 4, 8]-Yes, swap arr[0], arr[3] → [1, 2, 5, 3, 4, 8]
15i++ until arr[i]>=pivot133[1, 2, 5, 3, 4, 8]i=1arr[1]=2 <3 continue i=2
16i++ until arr[i]>=pivot233[1, 2, 5, 3, 4, 8]i=2arr[2]=5 >=3 stop
17j-- until arr[j]<=pivot223[1, 2, 5, 3, 4, 8]j=2arr[2]=5 >3 continue j=1
18i < j? i=2, j=1213[1, 2, 5, 3, 4, 8]-No, return j=1
19Partition done, pivot index = 1213[1, 2, 5, 3, 4, 8]-Hoare partition returns 1
💡 Lomuto ends after swapping pivot to correct position; Hoare ends when i >= j and returns j.
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 7After Step 8After Step 9After Step 14After Step 18
i (Lomuto)-101334---
j (Lomuto)-024-----
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][3,2,5,1,4,8]---
i (Hoare)-------102
j (Hoare)------631
Array (Hoare)------[3,2,5,1,4,8][1,2,5,3,4,8][1,2,5,3,4,8]
Key Moments - 3 Insights
Why does Lomuto use i = low - 1 instead of starting at low?
Because i tracks the last position where elements <= pivot are placed. Starting at low - 1 means no elements are placed yet. See execution_table step 2 where i increments before swapping.
In Hoare partition, why do we increment i and decrement j until conditions fail?
To find elements on wrong sides of pivot to swap. i moves right to find element >= pivot, j moves left to find element <= pivot. See execution_table steps 10-13 and 15-17.
Why does Hoare return j instead of i as pivot index?
Because when i >= j, j marks the boundary where left side <= pivot and right side >= pivot. Returning j ensures partitioning is correct. See execution_table step 18.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the array state after Lomuto step 6?
A[3, 8, 2, 5, 1, 4]
B[3, 2, 5, 1, 8, 4]
C[3, 2, 8, 5, 1, 4]
D[3, 2, 5, 1, 4, 8]
💡 Hint
Check the 'Array State' column at step 6 in execution_table.
At which step in Hoare partition does the first swap occur?
AStep 10
BStep 18
CStep 14
DStep 12
💡 Hint
Look for 'swap' action in execution_table rows for Hoare partition.
If in Lomuto partition the pivot was the first element instead of last, 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 i tracks last element <= pivot; pivot position affects i initialization (see variable_tracker).
Concept Snapshot
Quick Sort Partition:
- Lomuto: pivot = last element
- i starts at low-1, j scans low to high-1
- Swap elements <= pivot forward
- Swap pivot to i+1, return i+1

- Hoare: pivot = first element
- i=low-1, j=high+1
- Move i right until >= pivot, j left until <= pivot
- Swap arr[i], arr[j] if i<j else return j

Both rearrange array around pivot for recursive quicksort.
Full Transcript
This visualization shows how Lomuto and Hoare partition methods work in quicksort. Lomuto chooses the last element as pivot, initializes i to low-1, and moves j from low to high-1. When arr[j] is less or equal to pivot, i increments and swaps arr[i] and arr[j]. After the loop, pivot is swapped to position i+1, which is returned as the pivot index. Hoare chooses the first element as pivot, sets i to low-1 and j to high+1, then moves i right until arr[i] >= pivot and j left until arr[j] <= pivot. If i < j, arr[i] and arr[j] are swapped; otherwise, j is returned as the pivot index. The execution table traces each step, showing pointer movements, swaps, and array states. Key moments clarify why pointers start at certain positions and why Hoare returns j. The quiz tests understanding of array states and pointer logic. This helps learners see how partitioning rearranges elements for quicksort.

Practice

(1/5)
1. What is the main difference between Lomuto and Hoare partition schemes in Quick Sort?
easy
A. Lomuto uses a single pointer to partition, Hoare uses two pointers moving inward
B. Lomuto sorts the array in descending order, Hoare sorts in ascending order
C. Lomuto requires extra memory, Hoare works in-place
D. Lomuto always picks the last element as pivot, Hoare always picks the first

Solution

  1. Step 1: Understand Lomuto partition

    Lomuto partition uses one pointer to track the boundary of smaller elements and swaps elements accordingly.
  2. Step 2: Understand Hoare partition

    Hoare partition uses two pointers starting from both ends moving inward to swap elements around the pivot.
  3. Final Answer:

    Lomuto uses a single pointer to partition, Hoare uses two pointers moving inward -> Option A
  4. Quick Check:

    Partition pointers = single vs two [OK]
Hint: Remember: Lomuto single pointer, Hoare two pointers inward [OK]
Common Mistakes:
  • Confusing pivot selection with partition method
  • Thinking Lomuto uses two pointers
  • Assuming Hoare needs extra memory
2. Which of the following is the correct Lomuto partition function header in Go?
easy
A. func lomutoPartition(arr []int, low int, high int) int
B. func lomutoPartition(arr []int) int
C. func lomutoPartition(arr []int, pivot int) int
D. func lomutoPartition(arr []int, start int, end int) bool

Solution

  1. Step 1: Check parameters needed for Lomuto

    Lomuto partition requires the array and indices low and high to define the subarray.
  2. Step 2: Check return type

    It returns an integer index of the pivot after partitioning.
  3. Final Answer:

    func lomutoPartition(arr []int, low int, high int) int -> Option A
  4. Quick Check:

    Function signature = arr, low, high, returns int [OK]
Hint: Lomuto needs array and low, high indices, returns pivot index [OK]
Common Mistakes:
  • Omitting low and high parameters
  • Returning bool instead of int
  • Passing pivot as parameter explicitly
3. What is the output array after applying Lomuto partition on [8, 3, 7, 6, 2] with pivot as last element?
medium
A. [8, 3, 7, 6, 2]
B. [3, 2, 7, 6, 8]
C. [2, 3, 7, 6, 8]
D. [2, 3, 6, 7, 8]

Solution

  1. Step 1: Identify pivot and initialize pointers

    Pivot is last element 2. Initialize i = low - 1 = -1.
  2. Step 2: Iterate and swap elements smaller than pivot

    Check elements: 8 > 2 no swap, 3 > 2 no swap, 7 > 2 no swap, 6 > 2 no swap. i stays -1. Finally swap pivot with arr[i+1] = arr[0].
  3. Final Answer:

    [2, 3, 6, 7, 8] -> Option D
  4. Quick Check:

    Pivot 2 placed at start, smaller elements left [OK]
Hint: Pivot last element, swap smaller left, pivot next [OK]
Common Mistakes:
  • Swapping elements incorrectly
  • Not updating i pointer properly
  • Confusing pivot position after partition
4. Given this Hoare partition code snippet in Go, what is the bug?
func hoarePartition(arr []int, low, high int) int {
    pivot := arr[low]
    i := low - 1
    j := high + 1
    for {
        for arr[i] < pivot {
            i++
        }
        for arr[j] > pivot {
            j--
        }
        if i >= j {
            return j
        }
        arr[i], arr[j] = arr[j], arr[i]
    }
}
medium
A. The inner loops should use <= and >= instead of < and >
B. Pivot should be arr[high] instead of arr[low]
C. i and j initialization should be i = low, j = high
D. The swap should happen after checking i > j

Solution

  1. Step 1: Check pointer initialization

    In Hoare partition, i starts at low and j at high, not outside the array bounds.
  2. Step 2: Understand loop conditions

    Starting i at low - 1 and j at high + 1 causes index out of range errors during arr[i] and arr[j] access.
  3. Final Answer:

    i and j initialization should be i = low, j = high -> Option C
  4. Quick Check:

    Pointer start = low, high [OK]
Hint: Hoare pointers start at low and high, not outside [OK]
Common Mistakes:
  • Initializing pointers outside array bounds
  • Using wrong pivot element
  • Swapping before pointer check
5. You want to sort an array with many duplicate elements efficiently. Which partition scheme is better and why?
hard
A. Lomuto, because it handles duplicates by placing them all on one side
B. Hoare, because it partitions elements more evenly and reduces swaps with duplicates
C. Lomuto, because it uses two pointers to handle duplicates quickly
D. Hoare, because it always picks the last element as pivot

Solution

  1. Step 1: Consider duplicate handling in Lomuto

    Lomuto tends to put duplicates on one side, causing unbalanced partitions and more recursive calls.
  2. Step 2: Consider duplicate handling in Hoare

    Hoare's two-pointer approach partitions more evenly, reducing swaps and improving performance with duplicates.
  3. Final Answer:

    Hoare, because it partitions elements more evenly and reduces swaps with duplicates -> Option B
  4. Quick Check:

    Duplicates handled better by Hoare [OK]
Hint: Hoare partitions evenly, better for duplicates [OK]
Common Mistakes:
  • Assuming Lomuto is better for duplicates
  • Confusing pivot choice with partition efficiency
  • Ignoring swap count impact
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard