Mastery Points
0

Selection Sort in dsa

Context & Logic

Selection Sort finds the minimum element and puts it at the beginning.

Example

function selectionSort(arr) {
    for (let i = 0; i < arr.length; i++) {
        let minIdx = i;
        for (let j = i + 1; j < arr.length; j++) if (arr[j] < arr[minIdx]) minIdx = j;
        [arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];
    }
    return arr;
}

Step-by-Step Logic

1

Find the smallest element in the remaining unsorted part.

2

Swap it with the element at the current position.

Complexity Metrics

Time Efficiency

O(n²)

Memory Footprint

O(1)