0
0
DSA C++programming~20 mins

Sorting Stability and When to Use Which Sort in DSA C++ - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sorting Stability Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Stable Sort on Structs
Given the following C++ code that sorts a vector of structs by the 'value' field using a stable sort, what will be the printed output?
DSA C++
struct Item {
    int id;
    int value;
};

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<Item> items = {{1, 5}, {2, 3}, {3, 5}, {4, 2}};
    std::stable_sort(items.begin(), items.end(), [](const Item& a, const Item& b) {
        return a.value < b.value;
    });
    for (const auto& item : items) {
        std::cout << item.id << "-" << item.value << " ";
    }
    return 0;
}
A4-2 2-3 1-5 3-5
B4-2 2-3 3-5 1-5
C2-3 4-2 1-5 3-5
D2-3 4-2 3-5 1-5
Attempts:
2 left
💡 Hint
Stable sort keeps the original order of equal elements.
🧠 Conceptual
intermediate
1:30remaining
When to Prefer Stable Sorts?
Which scenario best explains when you should prefer a stable sorting algorithm over an unstable one?
AWhen you need to maintain the relative order of records with equal keys after sorting.
BWhen you want the fastest possible sorting regardless of order of equal elements.
CWhen sorting small arrays where stability does not matter.
DWhen you want to sort in descending order only.
Attempts:
2 left
💡 Hint
Think about preserving order of equal items.
Predict Output
advanced
2:00remaining
Output of Unstable Sort on Same Data
What is a possible output of the following C++ code using std::sort (which is not guaranteed to be stable) on the same data as before?
DSA C++
struct Item {
    int id;
    int value;
};

#include <iostream>
#include <vector>
#include <algorithm>

int main() {
    std::vector<Item> items = {{1, 5}, {2, 3}, {3, 5}, {4, 2}};
    std::sort(items.begin(), items.end(), [](const Item& a, const Item& b) {
        return a.value < b.value;
    });
    for (const auto& item : items) {
        std::cout << item.id << "-" << item.value << " ";
    }
    return 0;
}
A2-3 4-2 1-5 3-5
B4-2 2-3 3-5 1-5
C4-2 2-3 1-5 3-5
D1-5 3-5 2-3 4-2
Attempts:
2 left
💡 Hint
Unstable sort may reorder equal elements arbitrarily.
🧠 Conceptual
advanced
1:30remaining
Choosing Sorting Algorithm Based on Data Size and Stability
You have a large dataset that must be sorted by multiple keys, preserving the order of previous sorts. Which sorting approach is best?
AUse a stable sort once on the most important key only.
BUse an unstable sort once on the most important key only.
CUse a stable sort multiple times, starting from the least important key to the most important key.
DUse an unstable sort multiple times on all keys.
Attempts:
2 left
💡 Hint
Think about preserving order across multiple keys.
🔧 Debug
expert
2:30remaining
Why Does This Sorting Code Fail to Preserve Order?
Consider this C++ code snippet that attempts to sort a vector of pairs by the second element while preserving the original order of pairs with equal second elements. Why does it fail to preserve order?
DSA C++
std::vector<std::pair<int,int>> v = {{1,2}, {2,1}, {3,2}, {4,1}};
std::sort(v.begin(), v.end(), [](auto& a, auto& b) {
    return a.second < b.second;
});
for (auto& p : v) {
    std::cout << "(" << p.first << "," << p.second << ") ";
}
ABecause the output loop is missing a newline.
BBecause the lambda comparator is incorrect and does not compare properly.
CBecause the vector is not initialized correctly.
DBecause std::sort is not stable, it can reorder pairs with equal second elements arbitrarily.
Attempts:
2 left
💡 Hint
Check if the sorting algorithm preserves order of equal keys.