/**
* 冒泡排序基础
* arry:数组,必传
* type:排序方式,默认值1,1:升序,0:降序
**/
function bubbleSort(arry,type = 1){
if(Object.prototype.toString.call(arry) !== '[object Array]'){//判断是否数组
return false;
}
for (let i=0;i<arry.length;i++){
for(let j=0;j<arry.length-i;j++){
if((type == 1 && arry[j]>arry[j+1])||(type == 0 && arry[j]<arry[j+1])){
let temp = arry[j];
arry[j] = arry[j+1];
arry[j+1] = temp;
}
}
}
return arry;
}
网友评论