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.
How does the number of steps increase when sorting bigger lists?
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;
}
}
std::swap(arr[i], arr[minIndex]);
}
}
This code sorts an array by repeatedly finding the smallest element and moving it to the front.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Comparing elements inside the inner loop.
- How many times: The inner loop runs fewer times each pass, but overall about n*(n-1)/2 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 work.
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 compares elements the same way, so it takes the same time regardless of initial order.
Understanding Selection Sort's time complexity helps you compare it with faster sorting methods and shows your grasp of 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 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 CQuick Check:
Selection Sort = Find minimum and swap [OK]
- Confusing with Bubble Sort swapping adjacent elements
- Thinking it divides array like Merge Sort
- Assuming it uses extra data structures like heaps
arr[i] and arr[min_idx] in C++ during Selection Sort?Solution
Step 1: Identify correct swap function usage
In C++, the standard library providesswap(a, b)to exchange values of two variables.Step 2: Check syntax correctness
swap(arr[i], arr[min_idx]); usesswap(arr[i], arr[min_idx]);which correctly swaps the two elements.Final Answer:
swap(arr[i], arr[min_idx]); -> Option AQuick Check:
Use std::swap for swapping [OK]
- Assigning one element twice causing data loss
- Using Python-style tuple swap syntax in C++
- Confusing parameter order in swap function
arr = {64, 25, 12, 22, 11}?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}Step 2: Final sorted array
The array after sorting is[11, 12, 22, 25, 64].Final Answer:
[11, 12, 22, 25, 64] -> Option DQuick Check:
Selection Sort output = Sorted array ascending [OK]
- Not swapping minimum element correctly
- Confusing sorting order (descending vs ascending)
- Stopping before full array is sorted
for (int i = 0; i < n-1; i++) {
int min_idx = i;
for (int j = i+1; j < n; j++) {
if (arr[j] > arr[min_idx]) {
min_idx = j;
}
}
swap(arr[i], arr[min_idx]);
}Solution
Step 1: Analyze comparison logic
The code usesif (arr[j] > arr[min_idx])which finds the maximum, not minimum.Step 2: Correct comparison for Selection Sort
Selection Sort requires finding the minimum element, so the operator should be<.Final Answer:
The comparison operator should be < to find minimum -> Option BQuick Check:
Selection Sort finds minimum using < comparison [OK]
- Using > instead of < for minimum search
- Incorrect loop boundaries causing out-of-range errors
- Misusing swap function parameters
arr = {5, 3, 8, 6, 2}, how many swaps will Selection Sort perform to sort it in ascending order?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 swapStep 2: Count total swaps
Total swaps performed = 2Final Answer:
2 -> Option AQuick Check:
Count swaps only when minimum differs from current [OK]
- Counting all passes as swaps
- Ignoring when minimum is already at current position
- Assuming swaps equal number of passes
Ready to prove it?
Pass the test and this pattern moves unknown
