JS-排序详解-选择排序
varexample=[8,94,15,88,55,76,21,39
];
function selectSort(arr){
varlen=
arr.length;
var minIndex,temp;
console.time(‘选择排序耗时‘
);
for(i=0;i
){
minIndex=i;
for(j=i+1;j){
if(arr[j]<arr[minIndex]){
minIndex=j;
}
}
temp=arr[i];
arr[i]=arr[minIndex];
arr[minIndex]=temp;
}
console.timeEnd(‘选择排序耗时‘);
return arr;
}
console.log(selectSort(example));
来源: http://www.bubuko.com/infodetail-2013816.html