2.选择排序
let selectionSort = arr => {
for(let i=0,len=arr.length; i<len; i++) {
let minNumIndex = i;
for(let j=i+1; j<len; j++) {
if(arr[j] < arr[minNumIndex]) {
minNumIndex = j; // 每轮找出一个最小的index
}
}
[arr[i], arr[minNumIndex]] = [arr[minNumIndex], arr[i]];
}
return arr;
}
let arr1 = [1, 5, 3, 4, 7, 9, 2, 5];
console.log( selectionSort(arr1) );
网友评论