Bird
Raised Fist0
DSA Rustprogramming~20 mins

Quick Sort Partition Lomuto and Hoare in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Quick Sort Partition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of Lomuto Partition on a sample array
What is the output of the Lomuto partition function on the array after partitioning?
DSA Rust
fn lomuto_partition(arr: &mut [i32], low: usize, high: usize) -> usize {
    let pivot = arr[high];
    let mut i = low;
    for j in low..high {
        if arr[j] < pivot {
            arr.swap(i, j);
            i += 1;
        }
    }
    arr.swap(i, high);
    i
}

fn main() {
    let mut arr = [9, 3, 7, 6, 2, 8, 5];
    let p = lomuto_partition(&mut arr, 0, arr.len() - 1);
    println!("Partition index: {}", p);
    println!("Array after partition: {:?}", arr);
}
A
Partition index: 2
Array after partition: [3, 2, 5, 6, 9, 8, 7]
B
Partition index: 4
Array after partition: [3, 2, 5, 6, 7, 8, 9]
C
Partition index: 3
Array after partition: [2, 3, 5, 6, 7, 8, 9]
D
Partition index: 4
Array after partition: [2, 3, 5, 6, 7, 8, 9]
Attempts:
2 left
💡 Hint
Remember Lomuto uses the last element as pivot and places smaller elements before it.
❓ Predict Output
intermediate
2:00remaining
Output of Hoare Partition on a sample array
What is the output of the Hoare partition function on the array after partitioning?
DSA Rust
fn hoare_partition(arr: &mut [i32], low: usize, high: usize) -> usize {
    let pivot = arr[low];
    let mut i = low as isize - 1;
    let mut j = high as isize + 1;
    loop {
        loop {
            i += 1;
            if arr[i as usize] >= pivot {
                break;
            }
        }
        loop {
            j -= 1;
            if arr[j as usize] <= pivot {
                break;
            }
        }
        if i >= j {
            return j as usize;
        }
        arr.swap(i as usize, j as usize);
    }
}

fn main() {
    let mut arr = [8, 3, 7, 6, 2, 5, 4];
    let p = hoare_partition(&mut arr, 0, arr.len() - 1);
    println!("Partition index: {}", p);
    println!("Array after partition: {:?}", arr);
}
A
Partition index: 5
Array after partition: [4, 3, 7, 6, 2, 5, 8]
B
Partition index: 3
Array after partition: [4, 3, 2, 5, 6, 7, 8]
C
Partition index: 4
Array after partition: [4, 3, 2, 5, 6, 7, 8]
D
Partition index: 4
Array after partition: [3, 2, 4, 5, 6, 7, 8]
Attempts:
2 left
💡 Hint
Hoare partition uses the first element as pivot and swaps elements crossing from left and right.
🧠 Conceptual
advanced
1:30remaining
Difference between Lomuto and Hoare partition schemes
Which statement correctly describes a key difference between Lomuto and Hoare partition schemes?
AHoare partition is unstable, Lomuto partition is stable.
BLomuto partition always places the pivot in its final sorted position, Hoare may not.
CHoare partition requires extra space for temporary arrays, Lomuto does not.
DLomuto partition uses the first element as pivot, Hoare uses the last element.
Attempts:
2 left
💡 Hint
Think about where the pivot ends up after partitioning.
🔧 Debug
advanced
2:00remaining
Identify the error in this Lomuto partition implementation
What error will this Lomuto partition code cause when run?
DSA Rust
fn lomuto_partition(arr: &mut [i32], low: usize, high: usize) -> usize {
    let pivot = arr[high];
    let mut i = low - 1;
    for j in low..high {
        if arr[j] < pivot {
            i += 1;
            arr.swap(i, j);
        }
    }
    arr.swap(i + 1, high);
    i + 1
}

fn main() {
    let mut arr = [4, 5, 3, 7, 2];
    let p = lomuto_partition(&mut arr, 0, arr.len() - 1);
    println!("Partition index: {}", p);
    println!("Array after partition: {:?}", arr);
}
ANo error, outputs correct partition index and array
BInfinite loop due to incorrect loop range
CPanic due to out of bounds swap index
DPanic due to unsigned integer underflow at 'let mut i = low - 1;'
Attempts:
2 left
💡 Hint
Check the initialization of variable i when low is zero.
🚀 Application
expert
3:00remaining
Resulting array after full quicksort using Hoare partition
Given the array [10, 7, 8, 9, 1, 5], what is the final sorted array after applying quicksort with Hoare partition scheme?
DSA Rust
fn hoare_partition(arr: &mut [i32], low: usize, high: usize) -> usize {
    let pivot = arr[low];
    let mut i = low as isize - 1;
    let mut j = high as isize + 1;
    loop {
        loop {
            i += 1;
            if arr[i as usize] >= pivot {
                break;
            }
        }
        loop {
            j -= 1;
            if arr[j as usize] <= pivot {
                break;
            }
        }
        if i >= j {
            return j as usize;
        }
        arr.swap(i as usize, j as usize);
    }
}

fn quicksort(arr: &mut [i32], low: usize, high: usize) {
    if low < high {
        let p = hoare_partition(arr, low, high);
        if p > 0 {
            quicksort(arr, low, p);
        }
        quicksort(arr, p + 1, high);
    }
}

fn main() {
    let mut arr = [10, 7, 8, 9, 1, 5];
    let len = arr.len();
    quicksort(&mut arr, 0, len - 1);
    println!("Sorted array: {:?}", arr);
}
ASorted array: [5, 1, 7, 8, 9, 10]
BSorted array: [1, 5, 7, 9, 8, 10]
CSorted array: [1, 5, 7, 8, 9, 10]
DSorted array: [10, 9, 8, 7, 5, 1]
Attempts:
2 left
💡 Hint
Hoare partition quicksort sorts the array in ascending order.

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, Hoare uses two pointers moving inward
B. Lomuto sorts the array, Hoare reverses it
C. Lomuto works only on sorted arrays, Hoare on unsorted arrays
D. Lomuto uses recursion, Hoare uses iteration

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 that are out of place.
  3. Final Answer:

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

    Partition pointers = single vs two [OK]
Hint: Remember: Lomuto = one pointer, Hoare = two pointers [OK]
Common Mistakes:
  • Confusing which partition uses one or two pointers
  • Thinking both partitions use the same pointer strategy
  • Assuming Hoare partition sorts the array directly
2. Which of the following is the correct Rust function signature for Lomuto partition on a mutable slice of integers?
easy
A. fn lomuto_partition(arr: &mut Vec<i32>, low: i32, high: i32) -> usize
B. fn lomuto_partition(arr: &[i32], low: usize, high: usize) -> usize
C. fn lomuto_partition(arr: &mut [i32], low: usize, high: usize) -> usize
D. fn lomuto_partition(arr: &mut [i32], low: i32, high: i32) -> i32

Solution

  1. Step 1: Identify mutability and types

    Lomuto partition modifies the array, so it needs a mutable slice &mut [i32]. Indices are usize in Rust for slices.
  2. Step 2: Check return type

    Partition returns the pivot index, which is usize to match slice indexing.
  3. Final Answer:

    fn lomuto_partition(arr: &mut [i32], low: usize, high: usize) -> usize -> Option C
  4. Quick Check:

    Mutable slice + usize indices = fn lomuto_partition(arr: &mut [i32], low: usize, high: usize) -> usize [OK]
Hint: Mutable slice and usize indices for Rust array partitions [OK]
Common Mistakes:
  • Using immutable slice &[] when mutation is needed
  • Using i32 for indices instead of usize
  • Returning wrong type for pivot index
3. What is the output array after applying Lomuto partition on [8, 3, 7, 6, 2] with pivot as last element (2)?
medium
A. [2, 3, 7, 6, 8]
B. [2, 3, 7, 6, 8] with pivot index 0
C. [8, 3, 7, 6, 2]
D. [3, 2, 7, 6, 8]

Solution

  1. Step 1: Choose pivot and initialize pointers

    Pivot is last element 2. Start i at low-1 = -1 (conceptually), j iterates from low to high-1.
  2. Step 2: Iterate and swap

    Check each element: 8 > 2 no swap, 3 > 2 no swap, 7 > 2 no swap, 6 > 2 no swap. Finally swap pivot with element at i+1 = 0.
  3. Final Answer:

    [2, 3, 7, 6, 8] with pivot index 0 -> Option B
  4. Quick Check:

    Pivot 2 placed at index 0 [OK]
Hint: Pivot ends at correct position after swaps [OK]
Common Mistakes:
  • Not swapping pivot to correct place
  • Confusing pivot index with array values
  • Assuming array is fully sorted after partition
4. Identify the bug in this Hoare partition Rust code snippet:
fn hoare_partition(arr: &mut [i32], low: usize, high: usize) -> usize {
    let pivot = arr[low];
    let mut i = low - 1;
    let mut j = high + 1;
    loop {
        loop { i += 1; if arr[i] >= pivot { break; } }
        loop { j -= 1; if arr[j] <= pivot { break; } }
        if i >= j { return j; }
        arr.swap(i, j);
    }
}
medium
A. Pivot should be last element, not first
B. Loops should use <= and >= reversed
C. Swap should happen after checking i >= j
D. Using usize for i and j causes underflow when subtracting 1

Solution

  1. Step 1: Analyze pointer initialization

    i is initialized as low - 1 and j as high + 1, but usize cannot be negative, so low - 1 causes underflow.
  2. Step 2: Understand Rust usize behavior

    usize is unsigned, subtracting 1 from 0 wraps to max usize value, causing runtime errors or wrong behavior.
  3. Final Answer:

    Using usize for i and j causes underflow when subtracting 1 -> Option D
  4. Quick Check:

    Unsigned underflow = bug [OK]
Hint: Unsigned usize cannot go below zero [OK]
Common Mistakes:
  • Ignoring underflow in unsigned integers
  • Assuming pivot must be last element
  • Swapping before checking pointers cross
5. Given an array [10, 80, 30, 90, 40, 50, 70], which partition scheme (Lomuto or Hoare) will perform fewer swaps during the first partition step with pivot as last element (70)?
hard
A. Hoare partition performs fewer swaps
B. Cannot determine without full quicksort
C. Both perform the same number of swaps
D. Lomuto partition performs fewer swaps

Solution

  1. Step 1: Understand swap behavior of Lomuto

    Lomuto swaps every time it finds an element smaller than pivot, plus one final swap for pivot placement.
  2. Step 2: Understand swap behavior of Hoare

    Hoare swaps only when two pointers find elements on wrong sides, often fewer swaps than Lomuto.
  3. Step 3: Apply to given array

    For this array, Lomuto will swap multiple times moving smaller elements forward; Hoare will swap fewer pairs crossing pointers.
  4. Final Answer:

    Hoare partition performs fewer swaps -> Option A
  5. Quick Check:

    Hoare swaps fewer times [OK]
Hint: Hoare usually swaps less than Lomuto [OK]
Common Mistakes:
  • Assuming Lomuto always swaps less
  • Ignoring pointer crossing logic in Hoare
  • Thinking swap count equals number of elements
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