function mergeSort(array) {
  var compareFunction = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : defaultCompareFunction;
  var startIndex = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var endIndex = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : array.length;

  if (Math.abs(endIndex - startIndex) <= 1) {
    return [];
  }

  var middleIndex = Math.ceil((startIndex + endIndex) / 2);
  mergeSort(array, compareFunction, startIndex, middleIndex);
  mergeSort(array, compareFunction, middleIndex, endIndex);
  return merge(array, compareFunction, startIndex, middleIndex, endIndex);
}
Mergesort method which is recursively called for sorting the input array.

@param {Array} array The array which should be sorted.
@param {Function} compareFunction Compares two items in an array. If compareFunction is not supplied,
elements are sorted by converting them to strings and comparing strings in Unicode code point order.
@param {Number} startIndex Left side of the subarray.
@param {Number} endIndex Right side of the subarray.
@returns {Array} Array with sorted subarray.