zoukankan      html  css  js  c++  java
  • 排序算法

    const arr = [9, 8, 4, 2, 5, 7, 10, 1, 0, 88];
    const sort = (arr, desc) => {
    const _arr = [];
    for (let temp of arr) {
    if (_arr.length === 0) {
    _arr.push(temp);
    } else {
    if (temp <= _arr[0]) {
    _arr.unshift(temp);
    } else if (temp >= _arr[_arr.length - 1]) {
    _arr.push(temp);
    } else {
    for (let j in _arr) {
    if (temp > _arr[j] && temp < _arr[Number(j) + 1]) {
    _arr.splice(Number(j) + 1, 0, temp);
    break;
    }
    }
    }
    }
    }
    return desc ? _arr.reverse() : _arr;
    };

    console.time("sort");
    const asc = sort(JSON.parse(JSON.stringify(arr)));
    console.log(asc);
    console.timeEnd("sort");
    console.time("sortDesc");
    const desc = sort(JSON.parse(JSON.stringify(arr)), true);
    console.log(desc);
    console.timeEnd("sortDesc");

    document.getElementById("origin-div").innerHTML =
    "原始:" + JSON.stringify(arr);
    document.getElementById("asc-div").innerHTML = "升序:" + JSON.stringify(asc);
    document.getElementById("desc-div").innerHTML = "降序:" + JSON.stringify(desc);

  • 相关阅读:
    Mysql数据操作指令
    Mysql列属性
    Mysql表的对应关系
    Mysql中的一些类型
    Mysql笔记
    (三) rest_framework 权限与限流源码梳理
    (二) rest_framework 认证源码流程与配置
    (一) rest_framework 视图入口
    django_celery_beat
    GRPC
  • 原文地址:https://www.cnblogs.com/hustshu/p/15516242.html
Copyright © 2011-2022 走看看