编程语言
首页 > 编程语言> > JS排序算法

JS排序算法

作者:互联网

冒泡排序

    <script>
      let arr = [3, 5, 6, 10, 2, 1]
      //冒泡排序:相邻两个值作比较  将较大的换到后面  将较小的换到前面
      //i=0     i<6    arr[0] 3    arr[1] 5 ====>[3,5,6,10,2,1]
      //i=1     i<6    arr[1] 5    arr[2] 6 ====>[3,5,6,10,2,1]
      //i=2     i<6    arr[2] 6    arr[3] 10===>[3,5,6,10,2,1]
      //i=3     i<6    arr[3] 10   arr[4] 2 ====>[3,5,6,2,10,1]
      //i=4     i<6    arr[4] 10   arr[5] 1 ===>[3,5,6,2,1,10]
      //i=5     i<6    arr[5] 1    arr[6] undefined  ====>[3,5,6,2,1,10]
      
      for (let j = 1; j < arr.length; j++){
        for(let i = 0; i < arr.length-j; i++){
          if (arr[i] > arr[i + 1]){
            var middle = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = middle
          }
        }
      }
    </script>

冒泡排序过程

  for (let i = 0; i < arr.length - 1; i++){
          if (arr[i] > arr[i + 1]) {
            var middle = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = middle
          }
        }

        for (let i = 0; i < arr.length -2; i++){
          if (arr[i] > arr[i + 1]) {
            var middle = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = middle
          }
        }
        for (let i = 0; i < arr.length -3; i++){
          if (arr[i] > arr[i + 1]) {
            var middle = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = middle
          }
        }
        for (let i = 0; i < arr.length -4; i++){
          if (arr[i] > arr[i + 1]) {
            var middle = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = middle
          }
        }
        for (let i = 0; i < arr.length -5; i++){
          if (arr[i] > arr[i + 1]) {
            var middle = arr[i]
            arr[i] = arr[i + 1]
            arr[i + 1] = middle
          }
        }

选择排序

<script>
      let arr = [1, 4, 3, 2, 5, 7, 6, 9, 8, 0]
      /* minIndex=0               
      1  i=1  i<10      arr[0] 1  arr[1] 4      minIndex=0 
      2  i=2  i<10      arr[0] 1  arr[2] 3      minIndex=0
      3  i=3  i<10      arr[0] 1  arr[3] 2      minIndex=0 
      4  i=4  i<10      arr[0] 1  arr[4] 5      minIndex=0
      5  i=5  i<10      arr[0] 1  arr[5] 7      minIndex=0 
      6  i=6  i<10      arr[0] 1  arr[6] 6      minIndex=0
      7  i=7  i<10      arr[0] 1  arr[7] 9      minIndex=0 
      8  i=8  i<10      arr[0] 1  arr[8] 8      minIndex=0 
      9  i=9  i<10      arr[0] 1  arr[9] 0      minIndex=9  [0,4,3,2,5,7,6,9,8,1]
      */

      /* minIndex=1
       1 i=2 i<10      arr[1] 4       arr[2] 3    minIndex =2              
       2 i=3 i<10      arr[2] 3       arr[3] 2    minIndex = 3       
       3 i=4 i<10      arr[3] 2       arr[4] 5    minIndex =3    
       4 i=5 i<10      arr[3]2        arr[5] 7    minIndex =3
       5 i=6 i<10      arr[3] 2       arr[6] 6    minIndex =3
       6 i=7 i<10      arr[3] 2       arr[7] 9    minIndex = 3
       7 i=8 i<10       arr[3] 2       arr[8] 8    minIndex =3
       8 i=9 i<10      arr[3] 2       arr[9] 1    minIndex = 9[0,1,3,2,5,7,6,9,8,4]
      9i=10 i<10 终止
      */

      for (let j = 0; j < arr.length - 1; j++) {
        let minIndex = j
        for (let i = j + 1; i < 10; i++) {
          if (arr[i] < arr[minIndex]) {
            minIndex = i
          }
        }
        let middle = arr[j]
        arr[j] = arr[minIndex]
        arr[minIndex] = middle
      }
      console.log(arr)
    </script>

选择排序过程

    let arr = [3, 1, 4, 2, 6, 5]

      //************************************************************************************
      let min_index = 0

      for (let i = 1; i < arr.length; i++) {
        if (arr[i] < arr[min_index]) {
          min_index = i
        }
      }
      var middle = arr[0]
      arr[0] = arr[min_index]
      arr[min_index] = middle
      //************************************************************************************
      min_index = 1
      for (let i = 2; i < arr.length; i++) {
        if (arr[i] < arr[min_index]) {
          min_index = i
        }
      }
      var middle = arr[1]
      arr[1] = arr[min_index]
      arr[min_index] = middle
      //************************************************************************************
      min_index = 2
      for (let i = 3; i < arr.length; i++) {
        if (arr[i] < arr[min_index]) {
          min_index = i
        }
      }
      var middle = arr[2]
      arr[2] = arr[min_index]
      arr[min_index] = middle
      //************************************************************************************
      min_index = 3
      for (let i = 4; i < arr.length; i++) {
        if (arr[i] < arr[min_index]) {
          min_index = i
        }
      }
      var middle = arr[3]
      arr[3] = arr[min_index]
      arr[min_index] = middle
 //************************************************************************************
      min_index = 4
      for (let i = 5; i < arr.length; i++) {
        if (arr[i] < arr[min_index]) {
          min_index = i
        }
      }
      var middle = arr[4]
      arr[4] = arr[min_index]
      arr[min_index] = middle

 //************************************************************************************

标签:index,arr,min,++,JS,middle,算法,let,排序
来源: https://www.cnblogs.com/Thantics/p/16573363.html