0
0
DSA C++programming~10 mins

Quick Sort Partition Lomuto and Hoare in DSA C++ - Interactive Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to select the pivot element in Lomuto partition.

DSA C++
int lomutoPartition(int arr[], int low, int high) {
    int pivot = arr[[1]];
    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;
}
Drag options to blanks, or click blank then click option'
Ahigh
Bhigh - 1
Clow + 1
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Choosing the pivot as the first element instead of the last.
Using an index outside the array bounds.
2fill in blank
medium

Complete the code to initialize the pointer for Hoare partition.

DSA C++
int hoarePartition(int arr[], int low, int high) {
    int pivot = arr[low];
    int i = [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]);
    }
}
Drag options to blanks, or click blank then click option'
Alow - 1
Bhigh
Chigh - 1
Dlow
Attempts:
3 left
💡 Hint
Common Mistakes
Initializing 'i' at low instead of low - 1.
Confusing the start positions of 'i' and 'j'.
3fill in blank
hard

Fix the error in the Lomuto partition swap condition.

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] [1] pivot) {
            i++;
            std::swap(arr[i], arr[j]);
        }
    }
    std::swap(arr[i + 1], arr[high]);
    return i + 1;
}
Drag options to blanks, or click blank then click option'
A>
B>=
C==
D<=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' instead of '<=' causing wrong partitioning.
Using '==' which only moves elements equal to pivot.
4fill in blank
hard

Fill both blanks to complete the Hoare partition loop conditions.

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] [1] pivot);
        do {
            j--;
        } while (arr[j] [2] pivot);
        if (i >= j) return j;
        std::swap(arr[i], arr[j]);
    }
}
Drag options to blanks, or click blank then click option'
A<=
B>=
C<
D>
Attempts:
3 left
💡 Hint
Common Mistakes
Using wrong comparison operators causing infinite loops.
Mixing up the directions of 'i' and 'j' pointers.
5fill in blank
hard

Fill all three blanks to complete the Lomuto partition swap and return.

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[1];
            std::swap(arr[i], arr[j]);
        }
    }
    std::swap(arr[i [2] 1], arr[[3]]);
    return i + 1;
}
Drag options to blanks, or click blank then click option'
A++
B+
Chigh
D-
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'i+' instead of 'i++' causing syntax errors.
Swapping with wrong indices causing incorrect partition.