📖 Scenario: You have a list of numbers that you want to arrange from smallest to largest, like organizing books by height on a shelf.
🎯 Goal: You will build a program that uses the Selection Sort Algorithm to sort a list of numbers in ascending order.
📋 What You'll Learn
Create an integer array called numbers with the exact values: 29, 10, 14, 37, 13
Create an integer variable called n to store the length of the numbers array
Use nested for loops with variables i and j to implement the selection sort logic
Swap elements in the array to sort it in ascending order
Print the sorted array elements separated by spaces
💡 Why This Matters
🌍 Real World
Sorting is used in many real-life situations like organizing data, arranging files, or preparing lists for easier searching.
💼 Career
Understanding sorting algorithms like selection sort is fundamental for software development and technical interviews.
Progress0 / 4 steps
1
Create the initial array
Create an integer array called numbers with these exact values: 29, 10, 14, 37, 13.
DSA C#
Hint
Use int[] numbers = {29, 10, 14, 37, 13}; to create the array.
2
Create a variable for the array length
Create an integer variable called n and set it to the length of the numbers array using numbers.Length.
DSA C#
Hint
Use int n = numbers.Length; to get the array size.
3
Implement the selection sort logic
Use nested for loops with variables i and j to find the smallest element and swap it with the element at index i. Use a variable minIndex to track the index of the smallest element.
DSA C#
Hint
Use two loops: outer loop with i, inner loop with j. Swap the smallest found element with the element at i.
4
Print the sorted array
Use a for loop with variable i to print each element of the numbers array separated by spaces on one line.
DSA C#
Hint
Use a for loop to print each number followed by a space using Console.Write.
Practice
(1/5)
1. What is the main idea behind the Selection Sort Algorithm?
easy
A. Divide the list into halves and sort each half separately
B. Use a pivot to partition the list into smaller parts
C. Swap adjacent elements if they are in the wrong order
D. Repeatedly find the smallest element and move it to the front
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 divide and conquer or adjacent swaps, selection sort directly selects the minimum each time.
Final Answer:
Repeatedly find the smallest element and move it to the front -> Option D
Quick Check:
Selection sort = find minimum repeatedly [OK]
Hint: Selection sort picks smallest element each pass [OK]
Common Mistakes:
Confusing selection sort with bubble sort
Thinking it divides the list like merge sort
Assuming it uses a pivot like quicksort
2. Which of the following is the correct syntax to swap two elements arr[i] and arr[minIndex] in C# during selection sort?
easy
A. arr[i], arr[minIndex] = arr[minIndex], arr[i];
B. arr[i] = arr[minIndex]; arr[minIndex] = arr[i];
C. temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp;
D. swap(arr[i], arr[minIndex]);
Solution
Step 1: Understand swapping in C#
C# does not support tuple unpacking or direct swap functions; we use a temporary variable to swap values.
Step 2: Check each option
temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; uses a temp variable correctly. arr[i] = arr[minIndex]; arr[minIndex] = arr[i]; overwrites arr[i] before assigning arr[minIndex]. swap(arr[i], arr[minIndex]); is invalid syntax. arr[i], arr[minIndex] = arr[minIndex], arr[i]; is Python style, not C#.
3. What will be the output of the following C# code snippet after applying selection sort?
int[] arr = {64, 25, 12, 22, 11};
// Apply selection sort here
for (int i = 0; i < arr.Length - 1; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.Length; j++) {
if (arr[j] < arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
Console.WriteLine(string.Join(", ", arr));
medium
A. 11, 12, 22, 25, 64
B. 64, 25, 12, 22, 11
C. 25, 12, 22, 11, 64
D. 11, 22, 12, 25, 64
Solution
Step 1: Trace the selection sort iterations
First pass finds 11 (smallest) and swaps with 64. Array: [11, 25, 12, 22, 64]. Second pass finds 12 and swaps with 25. Array: [11, 12, 25, 22, 64]. Third pass finds 22 and swaps with 25. Array: [11, 12, 22, 25, 64]. Fourth pass finds 25 (already in place). Final sorted array is [11, 12, 22, 25, 64].
Step 2: Confirm final output
The printed output is the sorted array joined by commas.
4. Identify the error in the following selection sort code snippet and choose the correct fix:
int[] arr = {3, 1, 4, 2};
for (int i = 0; i < arr.Length; i++) {
int minIndex = i;
for (int j = i + 1; j < arr.Length; j++) {
if (arr[j] > arr[minIndex]) {
minIndex = j;
}
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
medium
A. Change 'if (arr[j] > arr[minIndex])' to 'if (arr[j] < arr[minIndex])'
B. Change outer loop to 'for (int i = 0; i < arr.Length - 1; i++)'
C. Remove the swap code inside the loop
D. Initialize minIndex to 0 instead of i
Solution
Step 1: Analyze the comparison condition
The code uses 'arr[j] > arr[minIndex]' which finds the maximum instead of minimum, so it sorts in descending order, not ascending.
Step 2: Correct the comparison for ascending order
Changing the condition to 'arr[j] < arr[minIndex]' finds the minimum element as required by selection sort.
Final Answer:
Change 'if (arr[j] > arr[minIndex])' to 'if (arr[j] < arr[minIndex])' -> Option A
Quick Check:
Comparison must find minimum element [OK]
Hint: Use '<' to find minimum in selection sort [OK]
Common Mistakes:
Using '>' instead of '<' in inner loop
Looping one extra time causing index errors
Not swapping elements properly
5. Given an array int[] arr = {5, 3, 8, 6, 2, 7, 4, 1}, how many swaps will the selection sort algorithm perform to sort it in ascending order?
hard
A. 6
B. 5
C. 7
D. 8
Solution
Step 1: Understand swap count in selection sort
Selection sort swaps once per outer loop iteration if the minimum is not already at the current position.
Step 2: Count swaps for given array
Array length is 8, so outer loop runs 7 times (0 to 6). Each iteration finds minimum and swaps if needed. Here, minimum is at current index in one iteration, so total swaps are 5.
Final Answer:
5 -> Option B
Quick Check:
Swaps = number of times minimum is not at current index [OK]
Hint: Selection sort swaps at most n-1 times, but only when needed [OK]
Common Mistakes:
Counting swaps as number of elements
Assuming swaps happen every iteration
Confusing swaps with comparisons
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.