b02585b64137b2c81c11d3ad31bc9d1e3a34a686611240ea23efc2411b0028b51f4670ac6804b2aaee9ad29a38c1ae113cefcc906c4fdbe1b89d7a821fd894 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import LinkedList from '../dataStructures/linkedList';
  2. /**
  3. * Refactored implementation of mergeSort (part of javascript-algorithms project) by Github users:
  4. * mgechev, AndriiHeonia and lekkas (part of javascript-algorithms project - all project contributors
  5. * at repository website)
  6. *
  7. * Link to repository: https://github.com/mgechev/javascript-algorithms
  8. */
  9. /**
  10. * Specifies a function that defines the sort order. The array is sorted according to each
  11. * character's Unicode code point value, according to the string conversion of each element.
  12. *
  13. * @param a {*} first compared element.
  14. * @param b {*} second compared element.
  15. * @returns {Number}
  16. */
  17. const defaultCompareFunction = function(a, b) {
  18. // sort lexically
  19. const firstValue = a.toString();
  20. const secondValue = b.toString();
  21. if (firstValue === secondValue) {
  22. return 0;
  23. } else if (firstValue < secondValue) {
  24. return -1;
  25. }
  26. return 1;
  27. };
  28. /**
  29. * Mergesort method which is recursively called for sorting the input array.
  30. *
  31. * @param {Array} array The array which should be sorted.
  32. * @param {Function} compareFunction Compares two items in an array. If compareFunction is not supplied,
  33. * elements are sorted by converting them to strings and comparing strings in Unicode code point order.
  34. * @param {Number} startIndex Left side of the subarray.
  35. * @param {Number} endIndex Right side of the subarray.
  36. * @returns {Array} Array with sorted subarray.
  37. */
  38. export default function mergeSort(array, compareFunction = defaultCompareFunction, startIndex = 0, endIndex = array.length) {
  39. if (Math.abs(endIndex - startIndex) <= 1) {
  40. return [];
  41. }
  42. const middleIndex = Math.ceil((startIndex + endIndex) / 2);
  43. mergeSort(array, compareFunction, startIndex, middleIndex);
  44. mergeSort(array, compareFunction, middleIndex, endIndex);
  45. return merge(array, compareFunction, startIndex, middleIndex, endIndex);
  46. }
  47. /**
  48. * Devides and sort merges two subarrays of given array
  49. *
  50. * @param {Array} array The array which subarrays should be sorted.
  51. * @param {Number} startIndex The start of the first subarray.
  52. * This subarray is with end middle - 1.
  53. * @param {Number} middleIndex The start of the second array.
  54. * @param {Number} endIndex end - 1 is the end of the second array.
  55. * @returns {Array} The array with sorted subarray.
  56. */
  57. export function merge(array, compareFunction, startIndex, middleIndex, endIndex) {
  58. const leftElements = new LinkedList();
  59. const rightElements = new LinkedList();
  60. const leftSize = middleIndex - startIndex;
  61. const rightSize = endIndex - middleIndex;
  62. const maxSize = Math.max(leftSize, rightSize);
  63. const size = endIndex - startIndex;
  64. for (let i = 0; i < maxSize; i += 1) {
  65. if (i < leftSize) {
  66. leftElements.push(array[startIndex + i]);
  67. }
  68. if (i < rightSize) {
  69. rightElements.push(array[middleIndex + i]);
  70. }
  71. }
  72. let i = 0;
  73. while (i < size) {
  74. if (leftElements.first && rightElements.first) {
  75. if (compareFunction(leftElements.first.data, rightElements.first.data) > 0) {
  76. array[startIndex + i] = rightElements.shift().data;
  77. } else {
  78. array[startIndex + i] = leftElements.shift().data;
  79. }
  80. } else if (leftElements.first) {
  81. array[startIndex + i] = leftElements.shift().data;
  82. } else {
  83. array[startIndex + i] = rightElements.shift().data;
  84. }
  85. i += 1;
  86. }
  87. return array;
  88. };