Selection Sort divides the input array into a sorted prefix and unsorted suffix, iteratively selecting the minimum value from the unsorted segment and swapping it to the front.
Scan indices 0..4 to locate minimum element 11 at index 4. Swap arr[0] (64) with arr[4] (11).
[64, 25, 12, 22, 11] -> Min=11 -> Swap -> [11, 25, 12, 22, 64]Scan unsorted suffix indices 1..4. Minimum is 12 at index 2. Swap arr[1] (25) with arr[2] (12).
[11, 25, 12, 22, 64] -> Min=12 -> Swap -> [11, 12, 25, 22, 64]Scan unsorted suffix indices 2..4. Minimum is 22 at index 3. Swap arr[2] (25) with arr[3] (22).
Final Output: [11, 12, 22, 25, 64]Scan your hand for the smallest card and place it first. Then scan the rest for the next smallest and place it second.
250,000 steps (Quadratic)
O(N²) Quadratic Time
O(1) In-Place Space
Memory growth rate as N expands.
Relative efficiency rating for large N.
| Operation | Time Complexity | Space Complexity |
|---|---|---|
| Best / Worst Case | O(N²) | O(1) |
function selectionSort(arr) {
const n = arr.length;
for (let i = 0; i < n; i++) {
let minIdx = i;
for (let j = i + 1; j < n; j++) {
if (arr[j] < arr[minIdx]) minIdx = j;
}
[arr[i], arr[minIdx]] = [arr[minIdx], arr[i]];
}
return arr;
}Makes at most O(N) total swaps, ideal for EEPROM flash memory with limited write cycles.
Validate your conceptual understanding, operation mechanics, and core concepts of Selection Sort.
Selection Sort performs at most N swaps, making it superior when memory write operations are significantly more expensive than reads!
Array loaded. Trigger Selection Sort to view outer/inner loops.