📖 Scenario: You are helping a librarian organize a small shelf of books by their ID numbers. The IDs are stored in an array, and you want to sort them from smallest to largest so it's easier to find any book.
🎯 Goal: Build a program that sorts an array of book IDs using the selection sort algorithm. You will create the array, set up a variable to track the minimum index, implement the sorting logic, and finally print the sorted array.
📋 What You'll Learn
Create an integer array called book_ids with these exact values: 29, 10, 14, 37, 13
Create an integer variable called min_index to track the index of the smallest value during sorting
Use nested for loops with variables i and j to implement the selection sort algorithm
Swap elements in book_ids to sort the array in ascending order
Print the sorted array elements separated by spaces
💡 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 processing, and optimizing performance.
Progress0 / 4 steps
1
Create the array of book IDs
Create an integer array called book_ids with these exact values: 29, 10, 14, 37, 13
DSA C
Hint
Use int book_ids[5] = {29, 10, 14, 37, 13}; to create the array.
2
Create the variable to track minimum index
Create an integer variable called min_index inside main to track the index of the smallest value during sorting
DSA C
Hint
Declare int min_index; inside main.
3
Implement the selection sort algorithm
Use nested for loops with variables i and j to implement the selection sort algorithm on book_ids. Use min_index to track the smallest element index and swap elements to sort the array in ascending order.
DSA C
Hint
Use two for loops: outer loop with i from 0 to 3, inner loop with j from i+1 to 4. Update min_index when a smaller element is found. Swap after inner loop.
4
Print the sorted array
Use a for loop with variable i to print all elements of book_ids separated by spaces after sorting
DSA C
Hint
Use a for loop from 0 to 4 and printf("%d ", book_ids[i]); to print each element with a space.
Practice
(1/5)
1. What is the main idea behind the Selection Sort Algorithm?
easy
A. Divide the list and merge sorted parts
B. Swap adjacent elements if they are in wrong order
C. Repeatedly find the smallest element and move it to the front
D. Insert elements into their correct position one by one
Solution
Step 1: Understand selection sort process
Selection sort finds the smallest element in the unsorted part and swaps it with the first unsorted element.
Step 2: Compare with other sorting methods
Unlike bubble sort (swapping adjacent) or merge sort (divide and conquer), selection sort selects the minimum each time.
Final Answer:
Repeatedly find the smallest element and move it to the front -> Option C
Quick Check:
Selection sort = find minimum and swap [OK]
Hint: Selection sort picks smallest and swaps front [OK]
Common Mistakes:
Confusing selection sort with bubble sort
Thinking it sorts by inserting elements
Believing it divides and merges lists
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. arr[i], arr[min_idx] = arr[min_idx], arr[i];
B. temp = arr[i]; arr[i] = arr[min_idx]; arr[min_idx] = temp;
C. swap(arr[i], arr[min_idx]);
D. arr[i] = arr[min_idx]; arr[min_idx] = arr[i];
Solution
Step 1: Understand swapping in C
C does not have built-in swap; we use a temporary variable to hold one value during swap.
Step 2: Check each option
temp = arr[i]; arr[i] = arr[min_idx]; arr[min_idx] = temp; uses a temp variable correctly. arr[i] = arr[min_idx]; arr[min_idx] = arr[i]; overwrites arr[i] before saving. swap(arr[i], arr[min_idx]); is invalid syntax in C. arr[i], arr[min_idx] = arr[min_idx], arr[i]; is Python syntax, not C.