sortTest({
num:10,
sortName:"选择排序",
sortFun: selectionSort
})
function sortTest(op){
var option = Object.assign(op,{
num:1000,
sortName: '排序',
sortFun: null,
min:1,
max:200
})
var arr = [];
for(var n = 0;n < option.num;n++){
arr.push(Math.random*(option.max+1-option.min)+option.min)
}
if(typeof(option.sortFun) === 'function'){
var start = new Date();
arr = sortFun(arr);
var end = new Date();
console.log(option.sortName + '耗费时间为' + (end - start)/1000 + '秒');
if(!checkSort(arr)) console.log("排序不成功")
}
}
function checkSort(arr){
for(var i = 1;i < arr.lenght;i++){
if(arr[i] < arr[i-1]) return false
}
return true
}
function selectionSort(arr){
var aux ;
var n;
for(var i = 0;i < arr.length ;i++){
aux = arr[i];
n = i;
for(var j = i;j < arr.length; i++){
if(arr[j] < aux ){
aux = arr[j];
n = j;
}
}
if(n!=i){
arr[n] = arr[i];
arr[i] = aux;
}
}
return arr
}
console