0
0
DSA C++programming~20 mins

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

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Selection Sort Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Selection Sort on a small array
What is the output of the following C++ code after performing selection sort on the array?
DSA C++
int arr[] = {64, 25, 12, 22, 11};
int n = 5;
for (int i = 0; i < n - 1; i++) {
    int min_idx = i;
    for (int j = i + 1; j < n; j++) {
        if (arr[j] < arr[min_idx]) {
            min_idx = j;
        }
    }
    int temp = arr[min_idx];
    arr[min_idx] = arr[i];
    arr[i] = temp;
}
for (int i = 0; i < n; i++) {
    std::cout << arr[i] << " ";
}
A64 25 12 22 11
B25 12 22 11 64
C11 12 22 25 64
D11 22 12 25 64
Attempts:
2 left
💡 Hint
Selection sort finds the smallest element and swaps it with the first unsorted element.
🧠 Conceptual
intermediate
1:30remaining
Number of swaps in Selection Sort
How many swaps does the selection sort algorithm perform on an array of size n in the worst case?
An * (n - 1) / 2
Bn - 1
Cn
Dn * log n
Attempts:
2 left
💡 Hint
Selection sort swaps only once per outer loop iteration.
🔧 Debug
advanced
2:00remaining
Identify the bug in this selection sort code
What error will occur when running this selection sort code snippet?
DSA C++
int arr[] = {3, 1, 4, 2};
int n = 4;
for (int i = 0; i < n; i++) {
    int min_idx = i;
    for (int j = i + 1; j < n; j++) {
        if (arr[j] < arr[min_idx]) {
            min_idx = j;
        }
    }
    int temp = arr[min_idx];
    arr[min_idx] = arr[i];
    arr[i] = temp;
}
ARuntime error: Array index out of bounds
BNo error, sorts correctly
CCompilation error due to missing semicolon
DLogical error: does not sort the array
Attempts:
2 left
💡 Hint
Check the inner loop boundary condition carefully.
🚀 Application
advanced
1:30remaining
Selection Sort on Linked List
Which of the following statements about applying selection sort on a singly linked list is true?
ASelection sort can be applied by swapping node values without changing links.
BSelection sort requires reversing the linked list first.
CSelection sort cannot be applied to linked lists.
DSelection sort requires converting the list to an array first.
Attempts:
2 left
💡 Hint
Think about how swapping values differs from swapping nodes.
Predict Output
expert
2:30remaining
Output after partial selection sort iterations
Given the array and partial selection sort code below, what is the array state after the second iteration of the outer loop?
DSA C++
int arr[] = {29, 10, 14, 37, 13};
int n = 5;
for (int i = 0; i < 2; i++) {
    int min_idx = i;
    for (int j = i + 1; j < n; j++) {
        if (arr[j] < arr[min_idx]) {
            min_idx = j;
        }
    }
    int temp = arr[min_idx];
    arr[min_idx] = arr[i];
    arr[i] = temp;
}
// Print array
for (int i = 0; i < n; i++) {
    std::cout << arr[i] << " ";
}
A13 10 14 37 29
B10 14 13 37 29
C29 10 14 37 13
D10 13 14 37 29
Attempts:
2 left
💡 Hint
Trace the first two passes of selection sort carefully.