0
0
DSA C++programming~20 mins

Comparison Based vs Non Comparison Based Sorting in DSA C++ - Compare & Choose

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Counting Sort on a small array
What is the output of the following C++ code that uses Counting Sort on the array [4, 2, 2, 8, 3, 3, 1]?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void countingSort(vector<int>& arr) {
    int max_val = *max_element(arr.begin(), arr.end());
    vector<int> count(max_val + 1, 0);
    for (int num : arr) {
        count[num]++;
    }
    int index = 0;
    for (int i = 0; i <= max_val; i++) {
        while (count[i] > 0) {
            arr[index++] = i;
            count[i]--;
        }
    }
}

int main() {
    vector<int> arr = {4, 2, 2, 8, 3, 3, 1};
    countingSort(arr);
    for (int num : arr) {
        cout << num << " ";
    }
    return 0;
}
A1 2 3 4 8
B1 2 2 3 3 4 8
C8 4 3 3 2 2 1
D2 2 3 3 4 8 1
Attempts:
2 left
💡 Hint
Counting Sort counts occurrences and then rebuilds the array in sorted order.
🧠 Conceptual
intermediate
1:30remaining
Time Complexity of Comparison Based Sorting
Which of the following is the best known lower bound on the time complexity of any comparison based sorting algorithm for n elements?
AO(n)
BO(n^2)
CO(n log n)
DO(log n)
Attempts:
2 left
💡 Hint
Think about the decision tree model for comparisons.
Predict Output
advanced
2:30remaining
Output of Radix Sort on integers
What is the output of the following C++ code that uses Radix Sort on the array [170, 45, 75, 90, 802, 24, 2, 66]?
DSA C++
#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

void countingSortForRadix(vector<int>& arr, int exp) {
    int n = arr.size();
    vector<int> output(n);
    int count[10] = {0};

    for (int i = 0; i < n; i++)
        count[(arr[i] / exp) % 10]++;

    for (int i = 1; i < 10; i++)
        count[i] += count[i - 1];

    for (int i = n - 1; i >= 0; i--) {
        output[count[(arr[i] / exp) % 10] - 1] = arr[i];
        count[(arr[i] / exp) % 10]--;
    }

    for (int i = 0; i < n; i++)
        arr[i] = output[i];
}

void radixSort(vector<int>& arr) {
    int max_val = *max_element(arr.begin(), arr.end());
    for (int exp = 1; max_val / exp > 0; exp *= 10) {
        countingSortForRadix(arr, exp);
    }
}

int main() {
    vector<int> arr = {170, 45, 75, 90, 802, 24, 2, 66};
    radixSort(arr);
    for (int num : arr) {
        cout << num << " ";
    }
    return 0;
}
A802 170 90 75 66 45 24 2
B2 24 45 75 66 90 170 802
C24 2 45 66 75 90 170 802
D2 24 45 66 75 90 170 802
Attempts:
2 left
💡 Hint
Radix Sort sorts numbers digit by digit starting from least significant digit.
🧠 Conceptual
advanced
1:30remaining
Which sorting algorithm is NOT comparison based?
Among the following sorting algorithms, which one does NOT rely on comparing elements to sort the array?
ACounting Sort
BHeap Sort
CMerge Sort
DQuick Sort
Attempts:
2 left
💡 Hint
Counting Sort uses counting frequencies instead of comparisons.
🧠 Conceptual
expert
2:00remaining
Why can't comparison based sorting be faster than O(n log n)?
Why is it impossible for any comparison based sorting algorithm to have a worst-case time complexity better than O(n log n)?
ABecause the number of possible orderings of n elements is n!, and the decision tree height must be at least log2(n!) which is O(n log n)
BBecause computers cannot perform comparisons faster than O(n log n)
CBecause memory limitations prevent faster sorting
DBecause non comparison based sorting algorithms are always slower
Attempts:
2 left
💡 Hint
Think about how many different ways n elements can be arranged.