Selection Sort
123456789101112131415161718
const input = [99, 44, 6, 2, 1, 5, 63, 87, 283, 4, 0];
const selectionSort = (a) => {
for (let i = 0; i < a.length; i++) {
let smallest = i;
for (let j = i; j < a.length; j++) {
if (a[j] < a[smallest]) {
smallest = j;
}
}
let temp = a[i];
a[i] = a[smallest];
a[smallest] = temp;
}
return a;
};
console.log(selectionSort(input));
JavaScript
INFO