function bubbleSort(ary) {
for(var i =0;i
var flag = false;
for(var j = 0;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));
网友评论