function newsort(arr, left, right){
for(var i= left; i < right; ++i){
var min = i;
for (var j = i; j < right; ++j){
if (arr[min] > arr[
The selection sort algorithm says: repeatedly find the minimum of the unsorted array.
Recursive approach
Note: the second argument of selectionSort (i) is needed in order to sort elements of the unsorted array
function selectionSort(arr, i) {
if (i === 0) {
return arr;
}
const min = Math.min(...arr.filter((x, j) => j < i));
const index = arr.findIndex(x => x === min);
arr.splice(index, 1);
arr.push(min);
return selectionSort(arr, --i);
}
const unsortedArr = [5, 34, 5, 1, 6, 7, 9, 2, 100];
console.log('result', selectionSort(unsortedArr , unsortedArr.length))