冒泡排序
const arr = [3,3,134,12,312];
// 排序
Array.prototype.mysort = function (compare = (a,b)=>(a<b)){
const len = this.length;
let newArr = this;
// 相互交换
const exchange = (arr,a,b) => {
const temp = arr[a];
arr[a] = arr[b];
arr[b] = temp;
}
for(let i = 0; i<len; i++) {
for(let j = 0; j<len-1; j++) {
if(compare(newArr[i],newArr[j])){
exchange(newArr ,i,j);
}
}
}
return newArr;
}
831

被折叠的 条评论
为什么被折叠?



