Nodejs Array Merge Sort mergesort (arr, lo, hi)

Here you can find the source of mergesort (arr, lo, hi)

Method Source Code

'use strict';/*from   w w  w. ja v  a 2s .com*/

Array.prototype.swap = function (i, j) {
  const temp = this[i];
  this[i] = this[j];
  this[j] = temp;
}

function mergesort (arr, lo, hi) {
  function partition (lo, hi) {
    if (lo >= hi) return;

    const mid = Math.floor((hi - lo) / 2) + lo;
    partition(lo, mid);
    partition(mid + 1, hi);
    merge(lo, mid, hi);
  }

  function merge (lo, mid, hi) {
    if (lo >= hi || arr[mid] <= arr[mid + 1]) return;

    const buf = arr.slice(lo, mid + 1), bufLen = buf.length;
    let leftIndex = 0, rightIndex = mid + 1;

    for (let i = lo; i <= hi; i++) {
      if      (leftIndex >= bufLen) arr[i] = arr[rightIndex++];
      else if (rightIndex > hi)     arr[i] = buf[leftIndex++];
      else                          arr[i] = arr[rightIndex] < buf[leftIndex] ? arr[rightIndex++] : buf[leftIndex++];
    }
  }

  partition(0, arr.length - 1);
  return arr;
}

const testArr = [5,2,78,5,0,13,6];
console.log(mergesort(testArr));

Related

  1. mergeSort(arr)
    var mergeSort = function (arr) {
        var n = parseInt(arr.length/2);
        if(n < 1){
            return arr;
        }else{
            var arr1 = mergeSort(arr.slice(0, n));
            var arr2 = mergeSort(arr.slice(n));
        var a = [];
    ...
    
  2. 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);
    
  3. 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) {
    ...
    
  4. 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
    ...
    
  5. 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) {
    ...