function smallest(array) { let smallest = array[0]; let index = 0; for (let i = 1; i < array.length; i++) { if (array[i] < smallest) { smallest = array[i]; index = i; } } return index; } function selectionSort(array, selector) { let res = []; while (array.length) { const smallest = selector(array); res.push(array.splice(smallest, 1)[0]); } return res; } const notSortedArray = [32, 34, 244, 7, 45, 57, 234, 5, 90, 35, 3546, 567, 32]; const sorted = selectionSort(notSortedArray, smallest); console.log(sorted);