So I programmed a sorting algorithm. It works. The only problem is , it’s not letting me pass the tests. It complains about using the built-in sort method, which I’m clearly not using. Does anyone have an idea why this is. Help would be greatly appreciated*
function selectionSort(arr) {
const len = arr.length
for (let i = 0 ; i < len; i++) {
var indexOfmin = i; //Check min of the rest of the array, because the part of the arr which we already sorted will be ignored
for (let j = i + 1; j < len; j++) {
if (arr[j] < arr[indexOfmin]) indexOfmin = j
}
if (i !== indexOfmin) {
//swap
[ arr[i], arr[indexOfmin] ] = [ arr[indexOfmin], arr[i] ];
}
}
return arr;
}
selectionSort([1, 4, 2, 8, 345, 123, 43, 32, 5643, 63, 123, 43, 2, 55, 1, 234, 92]);
Your browser information:
User Agent is: Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:74.0) Gecko/20100101 Firefox/74.0
.
Challenge: Implement Selection Sort
Link to the challenge: