lib/merge-sort-bottom-up.js
function bottomUpMerge(
    array, leftPosition, chunkSize, workArray, compare) {
  var i;
  var rightPosition = leftPosition + chunkSize;
  var endPosition = Math.min(leftPosition + chunkSize * 2 - 1,
                             array.length - 1);
  var leftIndex = leftPosition;
  var rightIndex = rightPosition;

  for (i = 0; i <= endPosition - leftPosition; i++) {
    if (leftIndex < rightPosition &&
        (rightIndex > endPosition ||
        compare(array[leftIndex], array[rightIndex]) <= 0)) {
      workArray[i] = array[leftIndex++];
    } else {
      workArray[i] = array[rightIndex++];
    }
  }

  for (i = leftPosition; i <= endPosition; i++) {
    array[i] = workArray[i - leftPosition];
  }
}
function bottomUpMerge(
    array, leftPosition, chunkSize, workArray, compare) {
  var i;
  var rightPosition = leftPosition + chunkSize;
  var endPosition = Math.min(leftPosition + chunkSize * 2 - 1,
                             array.length - 1);
  var leftIndex = leftPosition;
  var rightIndex = rightPosition;

  for (i = 0; i <= endPosition - leftPosition; i++) {
    if (leftIndex < rightPosition &&
        (rightIndex > endPosition ||
        compare(array[leftIndex], array[rightIndex]) <= 0)) {
      workArray[i] = array[leftIndex++];
    } else {
      workArray[i] = array[rightIndex++];
    }
  }

  for (i = leftPosition; i <= endPosition; i++) {
    array[i] = workArray[i - leftPosition];
  }
}
function bottomUpMerge ( array , leftPosition , chunkSize , workArray , compare ) { var i ; var rightPosition = leftPosition + chunkSize ; var endPosition = Math . min ( leftPosition + chunkSize * 2 - 1 , array . length - 1 ) ; var leftIndex = leftPosition ; var rightIndex = rightPosition ; for ( i = 0 ; i <= endPosition - leftPosition ; i ++ ) { if ( leftIndex < rightPosition && ( rightIndex > endPosition || compare ( array [ leftIndex ] , array [ rightIndex ] ) <= 0 ) ) { workArray [ i ] = array [ leftIndex ++ ] ; } else { workArray [ i ] = array [ rightIndex ++ ] ; } } for ( i = leftPosition ; i <= endPosition ; i ++ ) { array [ i ] = workArray [ i - leftPosition ] ; } }
Merge a portion of the array.
@param {Array} array The array to merge.
@param {number} leftPosition The starting index of the array to merge.
@param {number} chunkSize The size of the portion of the array to merge.
@param {Array} workArray A work array the size of the array argument, this
parameter is for performance reasons so many arrays aren't created by the
algorithm.
@param {function} compare The compare function.
@returns The sorted array.
Merge a portion of the array .