Nodejs Array Merge Sort mergeSort()

Here you can find the source of mergeSort()

Method Source Code

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 ww .  j a va 2s.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];
}

Related

  1. mergesort(A)
    function merge(left, right) {
      var result = [];
      while(left.length > 0 || right.length > 0) {
        if(left.length > 0 && right.length > 0) {
          if(left[0] <= right[0]) {
            result.push(left.shift());
          } else {
            result.push(right.shift());
        } else if(left.length > 0) {
          result.push(left.shift());
        } else if(right.length > 0) {
          result.push(right.shift());
      return result;
    function mergesort(A) {
      if(A.length < 2) {
        return A;
      var middle = Math.ceil(A.length / 2);
      var left = A.splice(0, middle);
      var right = A;
      left = mergesort(left);
      right = mergesort(right);
      return merge(left, right);
    Array.prototype.mergeSort = function() {
      return mergesort(this);
    
  2. mergesort(lst)
    var arr = [2, 4, 6, 1, 3, 5, 8, 7];
    var mergesort = function (lst) {
        if (lst.length === 1) return lst;
        var L = lst.slice(0, lst.length / 2),
            R = lst.slice(lst.length / 2, lst.length);
        var left = mergesort(L),
            right = mergesort(R);
        return merge(left, right);
        function merge(left, right) {
    ...
    
  3. mergeSort(array)
    function mergeSort(array) {
      var merge = function (left, right) {
        var total = []
        while (left.length && right.length)
          total.push(left[0] <= right[0] ? left.shift() : right.shift())
        return total.concat(left.concat(right));
      var length = array.length;
      if (length < 2) return array
    ...
    
  4. mergeSort(array)
    function merge(left, right) {
      const merged = [];
      while (left.length > 0 && right.length > 0) {
        let nextItem = (left[0] < right[0]) ? left.shift() : right.shift();
        merged.push(nextItem);
      return merged.concat(left, right);
    function mergeSort(array) {
    ...
    
  5. 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());
            return merged.concat(left.concat(right));
        if (this.length < 2) return this;
    ...
    
  6. mergeSort()
    Array.prototype.mergeSort = function() {
      function merge(left, right) {
        var arr = []
        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
    
  7. mergeSort()
    Array.prototype.mergeSort = function () {
      if (this.length <= 1) {
        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 ary = [];
    ...
    
  8. mergeSort()
    Array.prototype.mergeSort = function () {  
      if (this.length <= 1)   
        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)   
    ...
    
  9. mergeSort()
    Array.prototype.mergeSort = function () {
      if ( this.length < 2 ) {
        return this;
      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();