Selection Sort Visualization
Speed:
Step: 0 / -1Algorithm Code
1function selectionSort(arr) {
2 const n = arr.length;
3
4 for (let i = 0; i < n - 1; i++) {
5 // Find the minimum element in the unsorted part
6 let minIndex = i;
7
8 for (let j = i + 1; j < n; j++) {
9 if (arr[j] < arr[minIndex]) {
10 minIndex = j;
11 }
12 }
13
14 // Swap the found minimum with the first element
15 if (minIndex !== i) {
16 [arr[i], arr[minIndex]] = [arr[minIndex], arr[i]];
17 }
18 }
19
20 return arr;
21 }Visualization