Nodejs Array Shell Sort shellSortBy(key)

Here you can find the source of shellSortBy(key)

Method Source Code

Array.prototype.shellSortBy = function(key) {
    function more(a, b) {
        if (a > b) {
            return true
        } else {//from w  w  w. j  a  v a  2 s  .  c om
            return false
        }
    }

    function exch(a, i, j) {
        var t = a[i]
        a[i] = a[j]
        a[j] = t
    }
    var l = this.length
    var h = 128
    while (h < l / 2) h = 2 * h + 1
    while (h >= 1) {
        for (var i = h; i < l; i++) {
            for (var j = i; j >= h; j -= h) {
                if (more(this[j][key], this[j - h][key])) {
                    exch(this, j, j - h)
                } else {
                    break
                }
            }
        }
        h = Math.floor(h / 2)
    }
    return this
}

Related

  1. shellSort()
    Array.prototype.shellSort = function () {
      var gap, i, j;
      var temp;
      for (gap = this.length >> 1; gap > 0; gap >>= 1) {
        for (i = gap; i < this.length; i++) {
          temp = this[i];
          for (j = i - gap; j >= 0 && this[j] > temp; j -= gap) {
            this[j + gap] = this[j];
          this[j + gap] = temp;
    };
    
  2. shellSort()
    Array.prototype.shellSort = function(){
      var temp,len = this.length;
      for(var increment = Math.floor(len/2);increment > 0;increment = Math.floor(increment/2)){
        for(var i = increment;i < len;i++){
          for(var j = i - increment;j >= 0&&this[j] > this[j + increment];j -= increment){
            temp = this[j];
            this[j] = this[j+increment];
            this[j+increment] = temp;
      return this;
    
  3. shellSort()
    Array.prototype.shellSort = function() {
        this.procedures = []
        var l = this.length
        var compare = 0
        var exchange = 0
        var comparet = Math.sqrt(l * l * l)
        var exchanget = '?'
        var list = [0, 1, 5, 19, 41, 109, 209, 505, 929, 2161, 3905, 8929, 16001, 36289, 64769, 146305, 260609]
        var listnum = 1
    ...
    
  4. shellSort()
    Array.prototype.shellSort = function () {
        for (var step = parseInt(this.length)/2; step > 0; step--) {
            for(var i = 0; i < step; i++) {
                for (var j = step+i; j < this.length; j += step) {
                    var k = j, value = this[k];
                    while(k >= step && value < this[k-step]){
                        this[k] = this[k-step];
                        k -= step;
                    this[k] = value;
    };
    
  5. shellSort2()
    Array.prototype.shellSort2 = function() {
        var l = this.length
        var compare = 0
        var exchange = 0
        var comparet = Math.sqrt(l * l * l)
        var exchanget = '?'
        var h = 128
        while (h < l / 2) h = 2 * h + 1
        while (h >= 1) {
    ...