0
0
DSA C++programming~10 mins

Sorting Stability and When to Use Which Sort 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 perform a stable sort using std::stable_sort.

DSA C++
#include <vector>
#include <algorithm>

std::vector<int> data = {4, 2, 5, 2, 3};
std::stable_sort(data.begin(), data.[1]());
Drag options to blanks, or click blank then click option'
Arend
Bbegin
Crbegin
Dend
Attempts:
3 left
💡 Hint
Common Mistakes
Using begin() twice instead of begin() and end()
Using reverse iterators which sort in reverse order
2fill in blank
medium

Complete the code to check if a sorting algorithm is stable by comparing original and sorted vectors.

DSA C++
#include <vector>
#include <algorithm>
#include <iostream>

struct Item {
    int key;
    char value;
};

std::vector<Item> items = {{1, 'a'}, {2, 'b'}, {1, 'c'}};

std::stable_sort(items.begin(), items.end(), [](const Item &a, const Item &b) {
    return a.key < b.key;
});

for (const auto& item : items) {
    std::cout << item.key[1];
}
Drag options to blanks, or click blank then click option'
A << '\n'
B << ' '
C << ','
D << '.'
Attempts:
3 left
💡 Hint
Common Mistakes
Printing keys without spaces causing output to be hard to read
Using newline which prints each key on a new line
3fill in blank
hard

Fix the error in the code that attempts to use std::sort to maintain stability.

DSA C++
#include <vector>
#include <algorithm>

struct Record {
    int id;
    int score;
};

std::vector<Record> records = {{1, 90}, {2, 90}, {3, 80}};

std::sort(records.begin(), records.end(), [](const Record &a, const Record &b) {
    return a.score [1] b.score;
});
Drag options to blanks, or click blank then click option'
A>
B<=
C<
D>=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<=' or '>=' in comparator causing runtime errors
Expecting std::sort to be stable
4fill in blank
hard

Fill both blanks to create a lambda comparator that sorts by score descending and then by id ascending.

DSA C++
auto cmp = [](const Record &a, const Record &b) {
    if (a.score [1] b.score) return true;
    if (a.score == b.score) return a.id [2] b.id;
    return false;
};
Drag options to blanks, or click blank then click option'
A>
B<
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '<' for score causing ascending order instead of descending
Using '>' for id causing descending order instead of ascending
5fill in blank
hard

Fill all three blanks to create a stable sort call that sorts a vector of pairs by first element ascending and second element descending.

DSA C++
std::stable_sort(pairs.begin(), pairs.end(), [](const auto &a, const auto &b) {
    if (a.first [1] b.first) return true;
    if (a.first [3] b.first) return a.second [2] b.second;
    return false;
});
Drag options to blanks, or click blank then click option'
A<
B>
C==
D!=
Attempts:
3 left
💡 Hint
Common Mistakes
Using '>' for first element causing descending order
Using '<' for second element causing ascending order
Using '!=' instead of '==' for equality check