Visualizers
Selection Sort Visualizer
Step-through animation of Selection Sort showing minimum search and swaps.
native (Canvas API)
Client-side
Ready to visualize.
Execution Log
Comparisons: 0
Swaps: 0
Current Action:
Idle
Legend:
Unsorted
Active Search
Current Min
Sorted
Selection Sort Algorithm
Selection Sort is an in-place comparison sorting algorithm. It is simple but generally inefficient on large lists, running in $O(n^2)$ time.
How It Works
- Find the minimum element in the unsorted part of the array.
- Swap it with the first unsorted element.
- Move the boundary between sorted and unsorted subarrays one element to the right.
- Repeat until the entire array is sorted.
Implementation
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;
}
}
// Swap with minimum element
const temp = arr[i];
arr[i] = arr[minIdx];
arr[minIdx] = temp;
}
return arr;
}