Selection Sort Algorithm in DSA C - Time & Space Complexity
We want to understand how the time taken by Selection Sort changes as the list size grows.
Specifically, how many steps does it take to sort a list of numbers?
Analyze the time complexity of the following code snippet.
void selectionSort(int arr[], int n) {
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 remaining element and swapping it to the front.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The inner loop that scans the unsorted part to find the minimum element.
- How many times: The outer loop runs (n-1) times, and for each, the inner loop runs fewer times, roughly (n-1), (n-2), ..., 1 times.
As the list size grows, the number of comparisons grows roughly with the square of the size.
| Input Size (n) | Approx. Comparisons |
|---|---|
| 10 | 45 |
| 100 | 4,950 |
| 1000 | 499,500 |
Pattern observation: Doubling the input size roughly quadruples the number of comparisons.
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 compare it with faster sorting methods and shows your grasp of basic algorithm efficiency.
"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 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 CQuick Check:
Selection sort = find minimum and swap [OK]
- Confusing selection sort with bubble sort
- Thinking it sorts by inserting elements
- Believing it divides and merges lists
arr[i] and arr[min_idx] in C during selection sort?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.Final Answer:
temp = arr[i]; arr[i] = arr[min_idx]; arr[min_idx] = temp; -> Option BQuick Check:
Use temp variable to swap in C [OK]
- Trying to swap without temp variable
- Using Python or other language syntax
- Overwriting values before saving
arr = {29, 10, 14, 37, 13}?Solution
Step 1: Find minimum in entire array
Minimum element is 10 at index 1.Step 2: Swap minimum with first element
Swap 29 (index 0) with 10 (index 1). Array becomes {10, 29, 14, 37, 13}.Final Answer:
{10, 29, 14, 37, 13} -> Option AQuick Check:
First pass swaps smallest to front [OK]
- Not swapping after finding minimum
- Swapping with wrong index
- Confusing first pass with full sort
for (int i = 0; i < n; i++) {
int min_idx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[min_idx])
min_idx = j;
}
arr[i] = arr[min_idx];
arr[min_idx] = arr[i];
}Solution
Step 1: Analyze swap lines
Lines arr[i] = arr[min_idx]; arr[min_idx] = arr[i]; overwrite arr[min_idx] with arr[i] after first assignment, losing original value.Step 2: Correct swap method
Use a temporary variable to hold arr[i], then assign arr[i] = arr[min_idx], then arr[min_idx] = temp to swap correctly.Final Answer:
Swap lines overwrite values; use a temp variable to swap -> Option AQuick Check:
Swap needs temp variable to avoid overwrite [OK]
- Swapping without temp variable
- Changing loop start incorrectly
- Removing necessary inner loop
arr = {64, 25, 12, 22, 11}, after fully sorting it using selection sort, what is the final sorted array?Solution
Step 1: Perform selection sort passes
Pass 1: min=11, swap with 64 -> {11,25,12,22,64}
Pass 2: min=12, swap with 25 -> {11,12,25,22,64}
Pass 3: min=22, swap with 25 -> {11,12,22,25,64}
Pass 4: min=25, no swap needed -> {11,12,22,25,64}Step 2: Confirm final sorted array
After all passes, array is sorted ascending: {11, 12, 22, 25, 64}.Final Answer:
{11, 12, 22, 25, 64} -> Option DQuick Check:
Selection sort fully sorts ascending [OK]
- Stopping after first pass
- Not swapping correctly
- Mixing up element positions
Ready to prove it?
Pass the test and this pattern moves unknown
