Selection Sort Algorithm Find Minimum Swap is an important DAA topic because it appears in real projects, debugging sessions, and interviews. Learn the meaning first, then connect it to a small working example so the rule does not stay abstract.
For this page, focus on what problem Selection Sort Algorithm Find Minimum Swap solves, where developers usually make mistakes, and how to verify the result. The audit note for this lesson was: limited checklist/practice/mistake/FAQ notes .
A strong understanding of Selection Sort Algorithm Find Minimum Swap should include syntax, behavior, one realistic use case, one failure case, and one quick way to check your work with tools or output.
Selection Sort Algorithm Find Minimum Swap should be studied as a practical algorithm analysis lesson, not as a label. Start by naming the input, the rule that changes the input, and the result a learner should be able to predict after reading the page.
In the daa > selection-sort page, the notes should connect the definition with a working scenario, a mistake that beginners actually make, and the exact check that proves the fix. That makes the topic useful for coding, debugging, and interview revision.
Selection Sort is a simple comparison-based sorting algorithm. It repeatedly selects the smallest element from the unsorted part of the array and places it at the next correct position in the sorted part.
The array is conceptually divided into two parts:
After each pass, one more element is guaranteed to be in its final correct position.
Selection Sort always scans the unsorted portion to find the minimum element, even if the array is already sorted. That means the number of comparisons does not improve for best-case input.
The total comparisons are:
(n - 1) + (n - 2) + ... + 1 = n(n - 1)/2
So the time complexity remains O(n^2) in the best, average, and worst cases.
| Metric | Best | Average | Worst | Space | Stable? |
|---|---|---|---|---|---|
| Time Complexity | O(n^2) | O(n^2) | O(n^2) | O(1) | No |
| Comparisons | Always n(n - 1) / 2 | - | - | ||
| Swaps | At most n - 1 | - | - |
This is an important point:
This makes Selection Sort useful in situations where writing to memory is expensive but comparisons are relatively cheap.
Consider the array [29, 10, 14, 37, 13].
import java.util.Arrays;
public class SelectionSort {
static void selectionSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
if (minIdx != i) {
int temp = arr[minIdx];
arr[minIdx] = arr[i];
arr[i] = temp;
}
}
}
static void selectionSortDesc(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
int maxIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] > arr[maxIdx]) {
maxIdx = j;
}
}
int temp = arr[maxIdx];
arr[maxIdx] = arr[i];
arr[i] = temp;
}
}
public static void main(String[] args) {
int[] arr = {29, 10, 14, 37, 13};
System.out.println("Before: " + Arrays.toString(arr));
selectionSort(arr);
System.out.println("After: " + Arrays.toString(arr));
int[] arr2 = {5, 3, 8, 1, 9};
selectionSortDesc(arr2);
System.out.println("Desc: " + Arrays.toString(arr2));
}
}
def selection_sort(arr):
n = len(arr)
for i in range(n - 1):
min_idx = i
for j in range(i + 1, n):
if arr[j] < arr[min_idx]:
min_idx = j
if min_idx != i:
arr[i], arr[min_idx] = arr[min_idx], arr[i]
return arr
def selection_sort_desc(arr):
n = len(arr)
for i in range(n - 1):
max_idx = i
for j in range(i + 1, n):
if arr[j] > arr[max_idx]:
max_idx = j
arr[i], arr[max_idx] = arr[max_idx], arr[i]
return arr
arr = [29, 10, 14, 37, 13]
print("Before:", arr)
selection_sort(arr)
print("After: ", arr)
#include <iostream>
#include <vector>
using namespace std;
void selectionSort(vector<int>& arr) {
int n = arr.size();
for (int i = 0; i < n - 1; i++) {
int minIdx = i;
for (int j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
if (minIdx != i) {
swap(arr[i], arr[minIdx]);
}
}
}
int main() {
vector<int> arr = {29, 10, 14, 37, 13};
selectionSort(arr);
for (int x : arr) cout << x << " ";
return 0;
}
function selectionSort(arr) {
const n = arr.length;
for (let i = 0; i < n - 1; i++) {
let minIdx = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) {
minIdx = j;
}
}
if (minIdx !== i) {
[arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];
}
}
return arr;
}
let arr = [29, 10, 14, 37, 13];
console.log("Before:", arr);
selectionSort(arr);
console.log("After: ", arr);
| Pass | Current Position | Minimum Found | Action | Array After Pass |
|---|---|---|---|---|
| 1 | 0 | 10 | Swap with 29 | [10, 29, 14, 37, 13] |
| 2 | 1 | 13 | Swap with 29 | [10, 13, 14, 37, 29] |
| 3 | 2 | 14 | No swap needed | [10, 13, 14, 37, 29] |
| 4 | 3 | 29 | Swap with 37 | [10, 13, 14, 29, 37] |
Selection Sort is generally not stable because swapping the minimum element with the current position can change the relative order of equal elements.
For example, if equal values carry hidden labels such as 5a and 5b, a swap may move 5b before 5a, changing their original order.
Selection Sort needs only a constant number of extra variables such as indices and a temporary swap variable. It does not require extra arrays, so its space complexity is O(1).
| Feature | Selection Sort | Bubble Sort | Insertion Sort |
|---|---|---|---|
| Best case | O(n^2) | O(n) | O(n) |
| Average / Worst | O(n^2) | O(n^2) | O(n^2) |
| Stable | No | Yes | Yes |
| Number of swaps | Small | Can be many | Usually many shifts |
| Best use case | When writes are expensive | Educational use | Small or nearly sorted data |
1. Define the input for Selection Sort Algorithm Find Minimum Swap.
2. Apply the rule from the lesson.
3. Compare the actual result with the expected result.
4. Record the fix if the result differs.
1. Try empty, missing, duplicate, or invalid data.
2. Identify where Selection Sort Algorithm Find Minimum Swap changes behavior.
3. Explain the safest correction.
4. Retest the normal path.
Memorizing Selection Sort Algorithm Find Minimum Swap without the situation where it is useful.
Connect Selection Sort Algorithm Find Minimum Swap to a concrete algorithm analysis task.
Testing Selection Sort Algorithm Find Minimum Swap only with the perfect input.
Include empty, missing, duplicate, incompatible, or failed cases when relevant.
Changing code before reading the visible symptom or error message.
Inspect the output, state, configuration, or stack trace connected to Selection Sort Algorithm Find Minimum Swap.
Memorizing Selection Sort Algorithm Find Minimum Swap without the situation where it is useful.
Connect Selection Sort Algorithm Find Minimum Swap to a concrete algorithm analysis task.
The common mistake is memorizing syntax without understanding when the behavior changes or fails.
Remember the problem it solves in algorithm analysis, then attach the syntax or steps to that problem.
You can predict the result of a small example, explain a failure case, and choose it over a nearby alternative for a clear reason.
They often copy the syntax but skip the state, input, dependency, selector, route, type, or configuration that controls the behavior.
Explore 500+ free tutorials across 20+ languages and frameworks.