Javascript Array mergeSort() method

Description

Javascript Array mergeSort() method


Array.prototype.mergeSort = function(){
    var merge = function(left,right){
        var merged = [];
        while(left.length && right.length){
            merged.push(left[0] <= right[0] ? left.shift():right.shift());
        }//w  w  w.j  a  v  a  2s. c  o  m
        return merged.concat(left.concat(right));
    }
    if (this.length < 2) return this;
    var mid = Math.floor(this.length/2);
    return merge(this.slice(0,mid).mergeSort(),this.slice(mid).mergeSort())
}

Javascript Array mergeSort()

Array.prototype.mergeSort = function () {
  if (this.length <= 1) {
    return this;//from   ww  w  . ja  v  a 2s  . com
  }

  var half = parseInt(this.length / 2);

  var left = this.slice(0, half).mergeSort();
  var right = this.slice(half, this.length).mergeSort();

  var merge = function (left, right) {
  var ary = [];

    while (left.length > 0 && right.length > 0) {
      ary.push((left[0] <= right[0]) ? left.shift() : right.shift());
    };

    return ary.concat(left).concat(right);
  };

  return merge(left, right);
};


var a = [12,5,23,432,5,62];
console.log(a.mergeSort());

Javascript Array mergeSort()

Array.prototype.mergeSort = function() {
  function merge(left, right) {
    var arr = []/*ww w  .java  2  s . c  o  m*/
    while(left.length && right.length) {
      if(left[0] < right[0]) {
        arr.push(left.shift())
      } else {
        arr.push(right.shift())
      }
    }
    return arr.concat(left, right)
  }

  var len = this.length

  if(len > 1) {
    var index = Math.floor(len/2)
    var left = this.slice(0, index)
    var right = this.slice(index)
    return merge(left.mergeSort(), right.mergeSort())
  } else {
    return this
  }
}

Javascript Array mergeSort()

Array.prototype.mergeSort = function() {
 var list = this;
 var list2 = [];//w ww  .  j ava2 s .c  o m
 console.log(list);
 console.log(list2);
 if (list.length > 2 && list.length > 2) {
  var half = Math.floor(list.length/2);
  list2 = list.splice(0, half);
  console.log("list 1");
  list.mergeSort();
  console.log("list 2");
  list2.mergeSort();
 }
 console.log("list2: " + list2);
 console.log("Before "+list);
 list = list.concat(list2);
 console.log(list);

 // console.log(list);
 // console.log(list2);

};
var l = [34,7,23,32,5,62];
console.log(l.mergeSort());

Javascript Array mergeSort()

Array.prototype.mergeSort = function(){
    var merge = function(left,right){
        var merged = [];
        while(left.length && right.length){
            merged.push(left[0] <= right[0] ? left.shift():right.shift());
        }/*from w w w .ja va2 s .c  o  m*/
        return merged.concat(left.concat(right));
    }
    var myarray = [];
    this.forEach(function(item){
        myarray.push([item]);
    });
    while(myarray.length > 1){
       myarray.push(merge(myarray.shift(),myarray.shift()))
    }
    return myarray[0];
}

Javascript Array mergeSort()

Array.prototype.mergeSort = function () {  
  if (this.length <= 1)   
  {  /*from   w  ww .  j  av  a 2  s . c o  m*/
    return this;  
  }  
  
  var half = parseInt(this.length / 2);  
  var left = this.slice(0, half).mergeSort();  
  var right = this.slice(half,this.length).mergeSort();  
  var merge = function (left, right)   
  {  
  var arry = [];  
  while (left.length > 0 && right.length > 0)  
  {  
    arry.push((left[0] <= right[0]) ? left.shift() : right.shift());  
  }  
    return arry.concat(left).concat(right);  
  };  
  
  return merge(left, right);  
};  
  
var a = [34,7,23,32,5,62,55];  
console.log(a.mergeSort());

Javascript Array mergeSort()

// Monkey Patch merge_sort onto the array class

Array.prototype.mergeSort = function () {

  if ( this.length < 2 ) {
    return this;//from w  ww. j a v  a 2 s  . c  o  m
  }

  var mid_idx = Math.floor( this.length / 2 )

  var left = this.slice(0,mid_idx).mergeSort();
  var right = this.slice(mid_idx).mergeSort();

  return merge(left, right);
}

function merge(left, right) {

  var result = [];

    while (left.length > 0 && right.length > 0) {
      var nextItem = (left[0] < right[0]) ? left.shift() : right.shift();
      result.push(nextItem);
    }

  return result.concat(left).concat(right);

}

console.log();
console.log( JSON.stringify( [].mergeSort() ) == JSON.stringify( [] )  );
console.log( JSON.stringify( [6,5,3,1,8,7,2,4].mergeSort() ) == JSON.stringify( [1,2,3,4,5,6,7,8] ) );
console.log();

Javascript Array mergeSort()

Array.prototype.mergeSort = function() {
  var merge = function(left, right) {
    var result = [];
    var leftIndex = 0;
    var rightIndex = 0;
    while (leftIndex < left.length && rightIndex < right.length) {
      if (left[leftIndex] < right[rightIndex]) {
        result.push(left[leftIndex++]);/*from w  w  w  .  j  a va 2  s .c o  m*/
      } else {
        result.push(right[rightIndex++]);
      }
    }
    return result.concat(left.slice(leftIndex)).concat(right.slice(rightIndex));
  }

  if (this.length < 2) {
    return this;
  }

  var middleIndex = Math.floor(this.length / 2);
  var left = this.slice(0, middleIndex);
  var right = this.slice(middleIndex)
  return merge(left.mergeSort(), right.mergeSort())
};

var testArray = [900, 400, 200, 100, 9872, 439];

Javascript Array mergeSort()

// Bottom-up merge sort, no recursion

/////from   w  ww.  j  a v  a2 s.  com
function merge(arr, lBegin, lEnd, rBegin, rEnd, outArr, outPos) {
 for (var i = lBegin, j = rBegin; i < lEnd || j < rEnd; ++outPos) {
  if (j >= rEnd) {
   outArr[outPos] = arr[i++];
  } else if (i >= lEnd) {
   outArr[outPos] = arr[j++];
  } else if (arr[i] <= arr[j]) {
   outArr[outPos] = arr[i++];
  } else {
   outArr[outPos] = arr[j++];
  }
 }
}

///
Array.prototype.mergeSort = function() {
 var arr = this,
  aux = new Array(this.length),
  width, i;

 var mergePair = function(lBegin, width) {
  var lEnd = lBegin + width,
   rBegin = lEnd;
   rEnd = Math.min(rBegin + width, arr.length);

  if (lEnd <= rEnd) {
   merge(arr, lBegin, lEnd, rBegin, rEnd, aux, lBegin);
  }
 }

 for (width = 1; width < this.length; width *= 2) {
  for (i = 0; i < this.length; i += 2*width) {
   mergePair(i, width);
  }
  
  arr = [aux, aux = arr][0];
 }
}



PreviousNext

Related