Javascript Array shellSort()

Description

Javascript Array shellSort()


'use strict';/*from  w  ww  .  java2s.c o  m*/

Array.prototype.shellSort = function shellSort() {
    const N = this.length;
    let h = 1;

    while (h < N / 3) h = 3 * h + 1;

    while (h >= 1) {

        for (let x = h; x < N; x++) {

            for (let y = x; y >= h && isLess(this[]))
        }
    }
}

Javascript Array shellSort()

Array.prototype.shellSort = function () {
  var gap, i, j;//from   ww w.  ja va 2 s . c  o m
  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;
    }
  }
};

Javascript Array shellSort()

Array.prototype.shellSort = function () {
 var len = this.length,
  gap = parseInt(len / 2);/*from   ww w.  j a v  a2  s  .  co m*/
 var i, j, tmp;
 while (gap > 0) {
  for (i = gap; i < len; i++) {
   tmp = this[i];
   j = i - gap;
   while (j >= 0 && tmp < this[j]) {
    console.log(j)
    this[j + gap] = this[j];
    j = j - gap;
   }
   this[j + gap] = tmp;
  }
  gap = parseInt(gap / 2);
 }
 return this;
}

var arr = [14, 6, 9, 4, 11];
var nArr = arr.shellSort();
console.log(nArr);

Javascript Array shellSort()

Array.prototype.shellSort = function(){

 var arr = this;//from www .j  av a  2  s .  c  o m

 var gap = Math.floor(arr.length/2);

 while(gap >= 1){
  for(var i = gap; i < arr.length; i++){
   if(arr[i] <= arr[i-gap]){
    var tmp = arr[i];

    for(var j = i-gap; j>=0 && arr[j] > tmp; j=j-gap){
     arr[j+gap] = arr[j];
    }

    arr[j+gap] = tmp;
   }

  }
  gap =  Math.floor(gap/2);
 }
 

 return arr;

};



PreviousNext

Related