Bird
Raised Fist0
DSA C++programming~10 mins

Selection Sort Algorithm in DSA C++ - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to swap two elements in the array.

DSA C++
int temp = arr[i];
arr[i] = arr[[1]];
arr[[1]] = temp;
Drag options to blanks, or click blank then click option'
Aj
Bi
CminIndex
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Swapping with the wrong index like j or i itself.
Forgetting to use a temporary variable for swapping.
2fill in blank
medium

Complete the code to find the minimum element's index in the unsorted part of the array.

DSA C++
for (int j = i + 1; j < n; j++) {
    if (arr[j] < arr[[1]]) {
        minIndex = j;
    }
}
Drag options to blanks, or click blank then click option'
AminIndex
Bi
Cj
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Comparing with arr[i] instead of arr[minIndex].
Using j as the index to compare with itself.
3fill in blank
hard

Fix the error in the loop condition to avoid going out of array bounds.

DSA C++
for (int i = 0; i < [1]; i++) {
    // selection sort logic
}
Drag options to blanks, or click blank then click option'
An
Bn / 2
Cn + 1
Dn - 1
Attempts:
3 left
💡 Hint
Common Mistakes
Using i < n causes unnecessary iteration.
Using i < n + 1 causes out of bounds error.
4fill in blank
hard

Fill both blanks to complete the inner loop and update the minimum index correctly.

DSA C++
for (int j = [1]; j < n; j++) {
    if (arr[j] < arr[[2]]) {
        minIndex = j;
    }
}
Drag options to blanks, or click blank then click option'
Ai + 1
Bi
CminIndex
D0
Attempts:
3 left
💡 Hint
Common Mistakes
Starting inner loop from 0 or i instead of i + 1.
Comparing with arr[i] instead of arr[minIndex].
5fill in blank
hard

Fill all three blanks to complete the code that populates a std::map mapping elements greater than 10 to their indices.

DSA C++
std::map<int, int> result;
for (int i = 0; i < arr.size(); i++) {
    if (arr[i] [3] 10) {
        result[[1]] = [2];
    }
}
Drag options to blanks, or click blank then click option'
Aarr[i]
Bi
C>
D<
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong comparison operator.
Swapping key and value in the map.

Practice

(1/5)
1. What is the main idea behind the Selection Sort algorithm?
easy
A. Divide the array into halves and sort each half recursively
B. Swap adjacent elements if they are in wrong order
C. Repeatedly find the minimum element and place it at the beginning
D. Use a heap data structure to sort elements

Solution

  1. Step 1: Understand Selection Sort process

    Selection Sort works by finding the smallest element in the unsorted part and swapping it with the first unsorted element.
  2. Step 2: Compare with other sorting methods

    Unlike swapping adjacent elements or dividing arrays, Selection Sort directly selects the minimum element each time.
  3. Final Answer:

    Repeatedly find the minimum element and place it at the beginning -> Option C
  4. Quick Check:

    Selection Sort = Find minimum and swap [OK]
Hint: Selection Sort picks minimum each pass [OK]
Common Mistakes:
  • Confusing with Bubble Sort swapping adjacent elements
  • Thinking it divides array like Merge Sort
  • Assuming it uses extra data structures like heaps
2. Which of the following is the correct syntax to swap two elements arr[i] and arr[min_idx] in C++ during Selection Sort?
easy
A. swap(arr[i], arr[min_idx]);
B. arr[i] = arr[min_idx]; arr[min_idx] = arr[i];
C. swap(arr[min_idx], arr[j]);
D. arr[i], arr[min_idx] = arr[min_idx], arr[i];

Solution

  1. Step 1: Identify correct swap function usage

    In C++, the standard library provides swap(a, b) to exchange values of two variables.
  2. Step 2: Check syntax correctness

    swap(arr[i], arr[min_idx]); uses swap(arr[i], arr[min_idx]); which correctly swaps the two elements.
  3. Final Answer:

    swap(arr[i], arr[min_idx]); -> Option A
  4. Quick Check:

    Use std::swap for swapping [OK]
Hint: Use std::swap(a, b) to swap in C++ [OK]
Common Mistakes:
  • Assigning one element twice causing data loss
  • Using Python-style tuple swap syntax in C++
  • Confusing parameter order in swap function
3. What will be the output array after running Selection Sort on arr = {64, 25, 12, 22, 11}?
medium
A. [11, 22, 12, 25, 64]
B. [64, 25, 12, 22, 11]
C. [25, 11, 12, 22, 64]
D. [11, 12, 22, 25, 64]

Solution

  1. Step 1: Perform Selection Sort passes

    Pass 1: Minimum is 11, swap with 64 -> {11, 25, 12, 22, 64}
    Pass 2: Minimum is 12, swap with 25 -> {11, 12, 25, 22, 64}
    Pass 3: Minimum is 22, swap with 25 -> {11, 12, 22, 25, 64}
    Pass 4: Minimum is 25, no swap needed -> {11, 12, 22, 25, 64}
  2. Step 2: Final sorted array

    The array after sorting is [11, 12, 22, 25, 64].
  3. Final Answer:

    [11, 12, 22, 25, 64] -> Option D
  4. Quick Check:

    Selection Sort output = Sorted array ascending [OK]
Hint: Selection Sort outputs sorted ascending array [OK]
Common Mistakes:
  • Not swapping minimum element correctly
  • Confusing sorting order (descending vs ascending)
  • Stopping before full array is sorted
4. Identify the error in the following Selection Sort code snippet:
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;
    }
  }
  swap(arr[i], arr[min_idx]);
}
medium
A. The inner loop condition should be j <= n
B. The comparison operator should be < to find minimum
C. The swap function is used incorrectly
D. The outer loop should run till i < n

Solution

  1. Step 1: Analyze comparison logic

    The code uses if (arr[j] > arr[min_idx]) which finds the maximum, not minimum.
  2. Step 2: Correct comparison for Selection Sort

    Selection Sort requires finding the minimum element, so the operator should be <.
  3. Final Answer:

    The comparison operator should be < to find minimum -> Option B
  4. Quick Check:

    Selection Sort finds minimum using < comparison [OK]
Hint: Use < to find minimum in Selection Sort [OK]
Common Mistakes:
  • Using > instead of < for minimum search
  • Incorrect loop boundaries causing out-of-range errors
  • Misusing swap function parameters
5. Given an array arr = {5, 3, 8, 6, 2}, how many swaps will Selection Sort perform to sort it in ascending order?
hard
A. 2
B. 3
C. 4
D. 5

Solution

  1. Step 1: Trace swaps during Selection Sort

    Pass 1: Minimum is 2, swap with 5 -> swap count = 1
    Array: {2, 3, 8, 6, 5}
    Pass 2: Minimum is 3, already at position -> no swap
    Pass 3: Minimum is 5, swap with 8 -> swap count = 2
    Array: {2, 3, 5, 6, 8}
    Pass 4: Minimum is 6, already at position -> no swap
  2. Step 2: Count total swaps

    Total swaps performed = 2
  3. Final Answer:

    2 -> Option A
  4. Quick Check:

    Count swaps only when minimum differs from current [OK]
Hint: Count swaps only when minimum differs from current [OK]
Common Mistakes:
  • Counting all passes as swaps
  • Ignoring when minimum is already at current position
  • Assuming swaps equal number of passes
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard