Nodejs Array Merge Sort mergeSort()

Here you can find the source of mergeSort()

Method Source Code

Array.prototype.mergeSort = function () {  
  if (this.length <= 1)   
  {  //w  w  w .  ja  v 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());

Related

  1. 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) {
    ...
    
  2. 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;
    ...
    
  3. 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));
        var myarray = [];
    ...
    
  4. 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
    
  5. 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 = [];
    ...
    
  6. 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();
    
  7. 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;
    ...
    
  8. 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
    
  9. 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