0
0
DSA C++programming~20 mins

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

Choose your learning style9 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 given array
What is the output array after applying Lomuto partition on the array [8, 3, 7, 6, 2, 5, 4] with pivot as last element?
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++;
            std::swap(arr[i], arr[j]);
        }
    }
    std::swap(arr[i + 1], arr[high]);
    return i + 1;
}

int main() {
    int arr[] = {8, 3, 7, 6, 2, 5, 4};
    int n = 7;
    int p = lomutoPartition(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A[3, 7, 6, 2, 5, 4, 8]
B[3, 2, 4, 5, 6, 7, 8]
C[3, 2, 4, 5, 6, 8, 7]
D[2, 3, 4, 5, 6, 7, 8]
Attempts:
2 left
💡 Hint
Remember Lomuto partition places pivot at correct position and all smaller elements to left.
Predict Output
intermediate
2:00remaining
Output of Hoare Partition on given array
What is the output array after applying Hoare partition on the array [9, 4, 8, 3, 1, 2, 5] with pivot as first element?
DSA C++
int hoarePartition(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;
        std::swap(arr[i], arr[j]);
    }
}

int main() {
    int arr[] = {9, 4, 8, 3, 1, 2, 5};
    int n = 7;
    int p = hoarePartition(arr, 0, n - 1);
    for (int i = 0; i < n; i++) {
        std::cout << arr[i] << " ";
    }
    return 0;
}
A[5, 4, 8, 3, 1, 2, 9]
B[2, 4, 3, 1, 5, 8, 9]
C[1, 2, 3, 4, 5, 8, 9]
D[4, 2, 3, 1, 5, 8, 9]
Attempts:
2 left
💡 Hint
Hoare partition uses 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?
ALomuto partition always uses the first element as pivot, Hoare uses the last element.
BHoare partition returns the final pivot index, Lomuto returns an index that may not be the pivot position.
CHoare partition requires extra space, Lomuto partition works in-place.
DLomuto partition places the pivot in its final sorted position, Hoare partition may not.
Attempts:
2 left
💡 Hint
Think about where the pivot ends after partitioning.
🔧 Debug
advanced
2:00remaining
Identify error in Lomuto partition code snippet
What error will this Lomuto partition code produce when run on array [5, 3, 8, 4, 2]? int lomutoPartition(int arr[], int low, int high) { int pivot = arr[high]; int i = low; for (int j = low; j < high; j++) { if (arr[j] <= pivot) { std::swap(arr[i], arr[j]); i++; } } std::swap(arr[i], arr[high]); return i; }
AOff-by-one error causing incorrect pivot placement
BRuntime error due to invalid array index
CNo error, code runs correctly
DInfinite loop due to wrong loop condition
Attempts:
2 left
💡 Hint
Check initial value of i and how it moves.
🚀 Application
expert
2:30remaining
Number of swaps in Hoare partition
Given array [10, 7, 8, 9, 1, 5] and pivot as first element, how many swaps does Hoare partition perform?
DSA C++
int hoarePartition(int arr[], int low, int high) {
    int pivot = arr[low];
    int i = low - 1;
    int j = high + 1;
    int swapCount = 0;
    while (true) {
        do {
            i++;
        } while (arr[i] < pivot);
        do {
            j--;
        } while (arr[j] > pivot);
        if (i >= j) return swapCount;
        std::swap(arr[i], arr[j]);
        swapCount++;
    }
}

int main() {
    int arr[] = {10, 7, 8, 9, 1, 5};
    int n = 6;
    int swaps = hoarePartition(arr, 0, n - 1);
    std::cout << swaps;
    return 0;
}
A2
B3
C4
D5
Attempts:
2 left
💡 Hint
Trace i and j pointers and count swaps carefully.