zoukankan      html  css  js  c++  java
  • 插入排序

    /*
      insert: 实现插入排序
        @params
          ary [array] 需要排序的数组
        @return
          [array] 排序后的新数组
    */
      function insert(ary) {
        // 1、准备一个新数组,先保存一个数据
        let temp = [];
        temp.push(ary[0]);
        // 2、从第二项开始依次比较
        for (let i = 1; i < ary.length; i++) {
          // A 是新的数据
          let A = ary[i];
          // 和 Temp 里的数据依次比较(从后向前比)
          for (let j = temp.length - 1; j >= 0; j--) {
            // 每一次要比较的手里的牌
            let B = temp[j];
            // 如果当前新数据 A 比要比较的数据 B 大了,把 A 放到 B 的后面
            if (A > B) {
              temp.splice(j + 1, 0, A);
              break;
            }
            // 已经比到第一项
            if (j === 0) {
              temp.unshift(A);
            }
          }
        }
        return  temp;
      }
    let ary = [12, 8, 24, 16, 1]
    ary = insert(ary);
    console.log(ary)
  • 相关阅读:
    高中数学运算能力训练题
    vue @click.native
    vue_qqmapdemo1
    vuxdemo1
    使用命令行打开vscode
    nextjs-demo
    material-ui里面的withStyles是什么?
    material(一)
    有趣的npx
    在macbookpro上开启ssh服务
  • 原文地址:https://www.cnblogs.com/HYTing/p/12626388.html
Copyright © 2011-2022 走看看