其他分享
首页 > 其他分享> > 冒泡排序~21.8.6

冒泡排序~21.8.6

作者:互联网

1.JS代码

    <script>         function PX(arry) {             for (i = 0; i < arry.length - 1; i++) {                 for (j = 0; j < arry.length - i - 1; j++) {                     if (arry[j] > arry[j + 1]) {                         var temp = arry[j + 1];                         arry[j + 1] = arry[j];                         arry[j] = temp;                     }                 }             }             return arry;         }         var re = PX([6,9,5,8,5,654,3,3,3,1,7,2,987,987,987,5,6521,3,6,1]);         console.log(re);     </script>

2.注意的要点

  1)冒泡的轮次为arry.length-1,每一轮交换的次数为arry.length - i - 1;

  2)要准备一个中间变量用于交换两个数组元素;

 

标签:temp,PX,987,冒泡排序,21.8,re,var,arry
来源: https://www.cnblogs.com/xi-zg/p/15108794.html