var arr=[311,42,33,5,21,6,7,312]; console.log(arr); for (var i=1;i<=arr.length-1;i++){//重在体现循环次数(前两位不用比较-->所以比较次数是length-1) for (var j=1;j<=arr.length-i;j++){//如下图(j=1是下图的1,arr.length-i是下图循环次数的表达式) if(arr[j-1]>arr[j]){//交换逻辑 var sub=arr[j-1]; arr[j-1]=arr[j]; arr[j]=sub; } } } console.log(arr);