0
0
DSA C++programming~20 mins

Quick Sort Algorithm in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Quick Sort Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Quick Sort Partition Step
What is the output array after the partition step in Quick Sort using the last element as pivot?
DSA C++
int arr[] = {8, 3, 7, 6, 2};
int low = 0, high = 4;
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]);
// Print array after partition
for (int k = 0; k <= high; k++) {
    std::cout << arr[k] << " ";
}
A2 3 6 7 8
B3 2 6 7 8
C2 3 7 6 8
D3 6 2 7 8
Attempts:
2 left
💡 Hint
Remember the partition places elements smaller than pivot to the left and larger to the right.
Predict Output
intermediate
2:00remaining
Final Sorted Array by Quick Sort
What is the final sorted array after applying Quick Sort on this array?
DSA C++
int arr[] = {10, 7, 8, 9, 1, 5};
int n = 6;
// Assume quickSort(arr, 0, n-1) is called
// Print sorted array
for (int i = 0; i < n; i++) {
    std::cout << arr[i] << " ";
}
A1 7 5 8 9 10
B10 9 8 7 5 1
C1 5 7 8 9 10
D5 1 7 8 9 10
Attempts:
2 left
💡 Hint
Quick Sort sorts the array in ascending order.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Quick Sort Partition Code
What error will this Quick Sort partition code cause when run?
DSA C++
int partition(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;
}
ANo error; works correctly
BInfinite loop during partition
CRuntime error due to invalid swap indices
DOff-by-one error causing incorrect partition
Attempts:
2 left
💡 Hint
Check how variable i is initialized and incremented.
🧠 Conceptual
advanced
2:00remaining
Quick Sort Worst Case Scenario
Which input array causes the worst-case time complexity for Quick Sort when using the last element as pivot?
AA random shuffled array
BAn already sorted array in ascending order
CAn array with all identical elements
DAn array sorted in descending order
Attempts:
2 left
💡 Hint
Worst case happens when pivot divides array very unevenly.
Predict Output
expert
3:00remaining
Output of Quick Sort with Custom Pivot Selection
What is the output array after running Quick Sort with pivot chosen as middle element on this array?
DSA C++
void quickSort(int arr[], int low, int high) {
    if (low < high) {
        int mid = low + (high - low) / 2;
        int pivot = arr[mid];
        int i = low, j = high;
        while (i <= j) {
            while (arr[i] < pivot) i++;
            while (arr[j] > pivot) j--;
            if (i <= j) {
                std::swap(arr[i], arr[j]);
                i++;
                j--;
            }
        }
        if (low < j) quickSort(arr, low, j);
        if (i < high) quickSort(arr, i, high);
    }
}

int arr[] = {4, 9, 4, 8, 7, 3};
int n = 6;
quickSort(arr, 0, n - 1);
for (int k = 0; k < n; k++) {
    std::cout << arr[k] << " ";
}
A3 4 4 7 8 9
B3 4 4 8 7 9
C4 3 4 7 8 9
D4 4 3 7 8 9
Attempts:
2 left
💡 Hint
Pivot is middle element; partition around it and recursively sort.