首页 > 代码库 > 冒泡排序

冒泡排序

<script type="text/javascript">
function bubbleSort(ary) {
for(var i =0;i<ary.length-1;i++){
var flag = false;
for(var j = 0;j<ary.length-1-i;j++){
if(ary[j]>ary[j+1]){
ary[j]=ary[j]+ary[j+1];
ary[j+1]=ary[j]-ary[j+1];
ary[j] =ary[j]-ary[j+1];
flag = true;
}
}
if(flag){
flag =false;
}else {
break;
}
}
return ary;
}
var arr=[2,51,3,1,7,8,78,89];
console.log(bubbleSort(arr));
</script>

冒泡排序