谁能看出这种选择排序的bug在哪里,怎么改进可以正确输出
//另一种选择排序算法
const find = (arr) => {
let index = 0;
let small = arr[0];
for (let i = 0; i < arr.length; i++) {
if (arr[i] < small) {
small = arr[i];
index = i;
}
}
console.log(index);
return index;
};
const sort = (arr2) => {
let newArr = [];
for (let i = 0; i < arr2.length; i++) {
let index = find(arr2);
let popArr = arr2.splice(index, 1);
newArr.push(popArr[0]);
}
return newArr;
};
console.log(sort(values));