What is Selection Sort?
Find the smallest remaining value and move it to the front, one position at a time.
Type a tool name, format or task
Find the smallest remaining value and move it to the front, one position at a time. Interactive step-by-step animation with JavaScript code, complexity and front-end use cases.
Loading animation...
Find the smallest remaining value and move it to the front, one position at a time. Scan the unsorted part to find the smallest value. Swap it with the first unsorted position. Repeat for the rest of the list. Time complexity: O(n²). Space complexity: O(1). Makes the fewest swaps of the simple sorts. A good first step to understand sorting before merge and quick sort.
Find the smallest remaining value and move it to the front, one position at a time.
Time: O(n²). Space: O(1).
Makes the fewest swaps of the simple sorts. A good first step to understand sorting before merge and quick sort.
No. This converter runs in your browser for ordinary use, so the input is not sent to a backend by this tool.
Keep working without searching again.
Selection sort finds the smallest remaining value and puts it at the front, one position at a time.
function selectionSort(arr) {for (let i = 0; i < arr.length - 1; i++) {let min = i;for (let j = i + 1; j < arr.length; j++) {if (arr[j] < arr[min]) min = j;}if (min !== i) [arr[i], arr[min]] = [arr[min], arr[i]];}return arr;}
Tip: use the arrow keys to step and the space bar to play or pause.