- 时间复杂度是O(n^2)(最坏的)
最好的是O(n)
<script>
function swap(arr,i,j){
let temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
return arr;
}
Array.prototype.bubble=function bubble(){
for(let i=0;i<this.length-1;i++){
let _this=this,
flag=false;
for(let j=0;j<_this.length-1-i;j++){
if(_this[j]>_this[j+1]){
swap(_this,j,j+1);
flag=true;
}
}
if(!flag) break;
flag=false;
}
return _this;
}
let arr=[2,1,5,7,11,16,15];
console.log(arr.bubble());
</script>