Selection Sort Algorithm in DSA C# - Time & Space Complexity
We want to understand how the time needed to sort a list using selection sort changes as the list gets bigger.
How does the number of steps grow when the input size increases?
Analyze the time complexity of the following code snippet.
for (int i = 0; i < n - 1; i++)
{
int minIndex = i;
for (int j = i + 1; j < n; j++)
{
if (arr[j] < arr[minIndex])
minIndex = j;
}
int temp = arr[minIndex];
arr[minIndex] = arr[i];
arr[i] = temp;
}
This code sorts an array by repeatedly finding the smallest element from the unsorted part and swapping it to the front.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop that compares elements to find the minimum value.
- How many times: The outer loop runs (n-1) times, and for each outer loop, the inner loop runs fewer times, roughly (n - i - 1) times.
As the list size grows, the number of comparisons grows roughly with the square of the size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 45 comparisons |
| 100 | About 4,950 comparisons |
| 1000 | About 499,500 comparisons |
Pattern observation: When the input size doubles, the number of operations roughly quadruples.
Time Complexity: O(n²)
This means the time to sort grows quickly as the list gets bigger, roughly proportional to the square of the list size.
[X] Wrong: "Selection sort gets faster if the list is already sorted."
[OK] Correct: Selection sort always checks all remaining elements to find the minimum, so it does the same work regardless of initial order.
Understanding selection sort's time complexity helps you appreciate why more efficient sorting methods are used in real projects, and shows your grasp of basic algorithm analysis.
"What if we changed the inner loop to stop early when no smaller element is found? How would the time complexity change?"
Practice
Selection Sort Algorithm?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 DQuick Check:
Selection sort = find minimum repeatedly [OK]
- Confusing selection sort with bubble sort
- Thinking it divides the list like merge sort
- Assuming it uses a pivot like quicksort
arr[i] and arr[minIndex] in C# during selection sort?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#.Final Answer:
temp = arr[i]; arr[i] = arr[minIndex]; arr[minIndex] = temp; -> Option CQuick Check:
Use temp variable to swap in C# [OK]
- Trying to swap without a temp variable
- Using Python or other language syntax
- Overwriting one element before saving it
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));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.Final Answer:
11, 12, 22, 25, 64 -> Option AQuick Check:
Selection sort output = sorted array [OK]
- Not swapping correctly during iterations
- Confusing indexes and swapping wrong elements
- Assuming original array stays unchanged
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;
}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 AQuick Check:
Comparison must find minimum element [OK]
- Using '>' instead of '<' in inner loop
- Looping one extra time causing index errors
- Not swapping elements properly
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?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 BQuick Check:
Swaps = number of times minimum is not at current index [OK]
- Counting swaps as number of elements
- Assuming swaps happen every iteration
- Confusing swaps with comparisons
Ready to prove it?
Pass the test and this pattern moves unknown
