Nodejs Array Merge Sort mergeSort(arr2, arr3)

Here you can find the source of mergeSort(arr2, arr3)

Method Source Code

// Merge sorted using 2way merge twice
Array.prototype.mergeSort = function(arr2, arr3) {
   return this.merge2way(arr2.merge2way(arr3));
}

var maxNumber = process.argv[2];
function getRandomArbitrary(min, max) {
   return Math.round(Math.random() * (max - min) + min);
}


var arr1 = [getRandomArbitrary(1, 5)],
   arr2 = [getRandomArbitrary(1, 5)],//from ww w.  j  a  v a  2s .com
   arr3 = [getRandomArbitrary(1, 5)];
for(var i = 0; i < maxNumber; i++) {
   arr1.push(getRandomArbitrary(1, 5) + arr1[i]);
   arr2.push(getRandomArbitrary(1, 5) + arr2[i]);
   arr3.push(getRandomArbitrary(1, 5) + arr3[i]);
}

Array.prototype.merge2way = function(arr2) {
   var result = [],
      i = j = k = 0;
   while(i < this.length && j < arr2.length) {
      if(this[i] <= arr2[j]) {
         result[k++] = this[i++];
      } else {
         result[k++] = arr2[j++];
      }
   }

   while(i < this.length) {
      result[k++] = this[i++];
   }

   while(j < arr2.length) {
      result[k++] = arr2[j++];
   }
   return result;
}


// Combine first, then sort
Array.prototype.combineSort = function(arr2, arr3) {
   return this.join(arr2, arr3).sort(function(a, b) {
      return a - b;
   });
}

// Compare all 3 lists at the same time, and then 2 if one is exhausted
Array.prototype.merge3Way = function(arr2, arr3) {
   var result = [],
      i = j = k = l = 0;

   while(i < this.length && j < arr2.length && k < arr3.length) {
      if( (this[i] <= arr2[j]) && (this[i] <= arr3[k]) ) {
         result[l++] = this[i++];
      }
      else if( (arr2[j] <= this[i]) && (arr2[j] <= arr3[k]) ) {
         result[l++] = arr2[j++];
      } else {
         result[l++] = arr3[k++];
      }
   }

   if(i < this.length && j < arr2.length) {
      return result.concat(this.slice(i).merge2way(arr2.slice(j)));
   } else if (i < this.length && k < arr3.length) {
      return result.concat(this.slice(i).merge2way(arr3.slice(k)));
   } else {
      return result.concat(arr2.slice(j).merge2way(arr3.slice(k)));
   }
}

var now = Date.now();
var result1 = arr1.mergeSort(arr2, arr3).join(', ');
console.log(Date.now() - now);

now = Date.now();
var result2 = arr1.combineSort(arr2, arr3).join(', ');
console.log(Date.now() - now);

now = Date.now();
var result3 = arr1.merge3Way(arr2, arr3).join(', ');
console.log(Date.now() - now);

Related

  1. 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)   
    ...
    
  2. 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();
    
  3. mergeSort()
    ;"use strict";
    var log=function(msg){console.log(msg);};
    var alert=function(msg){log(msg);};
    var arr = [3,6,7,5,3,6,2,9,1,5,33,-12,0,-122,-Infinity, 125, 33, 55, 77];
    Array.prototype.mergeSort = function () {
      function _merge(arrLeft, arrRight){
          var result = [],
          leftIdx = 0,
          rightIdx = 0;
    ...
    
  4. mergeSort()
    Array.prototype.mergeSort = function() {
        function sort(array) {
            var length = array.length,
                mid    = Math.floor(length * 0.5),
                left   = array.slice(0, mid),
                right  = array.slice(mid, length);
            if(length === 1) {
              return array;
            return merge(sort(left), sort(right));
        function merge(left, right) {
            var result = []
            for (var li=0,ri=0,i=0;i<left.length+right.length;i++){
                if (li < left.length && ri < right.length) {
                    if (less(left[li], right[ri])) {
                        result[i]=left[li]
                        li++
                        continue
                    else {
                        result[i]=right[ri]
                        ri++
                        continue
                if (li===left.length) {
                    result[i]=right[ri]
                    ri++
                    continue
                else {
                    result[i]=left[li]
                    li++
                    continue
            virtualize(result, 0)
            return result
        var newArray=sort(this)
        return newArray
    
  5. mergeSort()
    Array.prototype.mergeSort = function() {
        function sort(array) {
            var length = array.length,
                mid    = Math.floor(length * 0.5),
                left   = array.slice(0, mid),
                right  = array.slice(mid, length);
            if(length === 1) {
              return array;
            return merge(sort(left), sort(right));
        function merge(left, right) {
            var result = []
            for (var li=0,ri=0,i=0;i<left.length+right.length;i++){
                if (li < left.length && ri < right.length) {
                    if (less(left[li], right[ri])) {
                        result[i]=left[li]
                        li++
                        continue
                    else {
                        result[i]=right[ri]
                        ri++
                        continue
                if (li===left.length) {
                    result[i]=right[ri]
                    ri++
                    continue
                else {
                    result[i]=left[li]
                    li++
                    continue
            return result
        var newArray=sort(this)
        return newArray
    
  6. mergeSort(sortKey, direction)
    Array.prototype.mergeSort = function(sortKey, direction){
      var unsortedArray = this;
      if(unsortedArray.length < 2) return unsortedArray;
      var middle = Math.floor(unsortedArray.length/2);
      var leftSubArray = unsortedArray.slice(0,middle).mergeSort(sortKey, direction);
      var rightSubArray = unsortedArray.slice(middle).mergeSort(sortKey, direction);
      var sortedArray = merge(leftSubArray, rightSubArray);
      return sortedArray;
      function merge(left, right) {
    ...