📖 Scenario: You are helping a librarian organize a small shelf of books by their ID numbers. The librarian wants the books sorted from smallest to largest ID so they are easy to find.
🎯 Goal: Build a program that sorts a list of book IDs using the selection sort algorithm. You will create the list, set up a variable to track the smallest value, implement the sorting logic, and finally print the sorted list.
📋 What You'll Learn
Create a list of integers called book_ids with the exact values: 45, 12, 78, 34, 23
Create an integer variable called min_index to track the position of the smallest value
Use nested for loops with variables i and j to implement selection sort
Swap elements in book_ids to sort the list in ascending order
Print the sorted book_ids list in the format: 12 -> 23 -> 34 -> 45 -> 78 -> null
💡 Why This Matters
🌍 Real World
Sorting is a common task in organizing data, like arranging books, files, or records in order.
💼 Career
Understanding sorting algorithms like selection sort helps in software development, data analysis, and optimizing programs.
Progress0 / 4 steps
1
Create the list of book IDs
Create a list of integers called book_ids with these exact values: 45, 12, 78, 34, 23
DSA C++
Hint
Use std::vector<int> to create the list and initialize it with the given numbers.
2
Create the variable to track the smallest value index
Create an integer variable called min_index to track the position of the smallest value during sorting
DSA C++
Hint
Declare min_index as an integer without initializing it yet.
3
Implement the selection sort logic
Use nested for loops with variables i and j to implement selection sort on book_ids. Inside the outer loop, set min_index = i. Inside the inner loop, update min_index if a smaller value is found. After the inner loop, swap book_ids[i] and book_ids[min_index] if needed.
DSA C++
Hint
Use two loops: the outer loop picks the current position, the inner loop finds the smallest value's index. Swap after inner loop ends.
4
Print the sorted list
Print the sorted book_ids list in the format: 12 -> 23 -> 34 -> 45 -> 78 -> null. Use a for loop to print each element followed by ->, and print null at the end.
DSA C++
Hint
Use a loop to print each number followed by ->, then print null after the loop.
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
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.
Step 2: Compare with other sorting methods
Unlike swapping adjacent elements or dividing arrays, Selection Sort directly selects the minimum element each time.
Final Answer:
Repeatedly find the minimum element and place it at the beginning -> Option C
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
Step 1: Identify correct swap function usage
In C++, the standard library provides swap(a, b) to exchange values of two variables.
Step 2: Check syntax correctness
swap(arr[i], arr[min_idx]); uses swap(arr[i], arr[min_idx]); which correctly swaps the two elements.
Final Answer:
swap(arr[i], arr[min_idx]); -> Option A
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
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}
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
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
Step 2: Count total swaps
Total swaps performed = 2
Final Answer:
2 -> Option A
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 unknownstrongwhile earning +1 Grip toward your certificate.