zoukankan      html  css  js  c++  java
  • Array 原型扩展(快速排序,搅乱顺序)


    /// 快速快速排序算法
    Array.prototype.quickSort = function (left, right) {
    // left = left || 0;
    // right = right || this.length - 1;
    if (left < right) {
    var x = this[right],
    i = left - 1,
    temp;

    for (var j = left; j <= right; j++) {
    if (this[j] <= x) {
    i++;
    temp = this[i];
    this[i] = this[j];
    this[j] = temp;
    }
    }
    this.quickSort(left, i - 1);
    this.quickSort(i + 1, right);
    };
    }


    // 搅乱当前的数组(洗牌)shuffle an array Fisher-Yates style
    Array.prototype.shuffle = function () {
    var i = this.length;
    if (i !== 0) {
    while (--i) {
    var j = Math.floor(Math.random() * (i + 1));
    var temp = this[i];
    this[i] = this[j];
    this[j] = temp;
    }
    }
    };

  • 相关阅读:
    数据库表关联分析
    java异常信息分析
    版本问题
    项目
    EXCEL工具
    项目安全
    服务器环境
    vue公共
    Linux 文件权限
    nginx
  • 原文地址:https://www.cnblogs.com/wujiakun/p/5433341.html
Copyright © 2011-2022 走看看