mergeSort.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import defined from './defined.js';
  2. import DeveloperError from './DeveloperError.js';
  3. var leftScratchArray = [];
  4. var rightScratchArray = [];
  5. function merge(array, compare, userDefinedObject, start, middle, end) {
  6. var leftLength = middle - start + 1;
  7. var rightLength = end - middle;
  8. var left = leftScratchArray;
  9. var right = rightScratchArray;
  10. var i;
  11. var j;
  12. for (i = 0; i < leftLength; ++i) {
  13. left[i] = array[start + i];
  14. }
  15. for (j = 0; j < rightLength; ++j) {
  16. right[j] = array[middle + j + 1];
  17. }
  18. i = 0;
  19. j = 0;
  20. for (var k = start; k <= end; ++k) {
  21. var leftElement = left[i];
  22. var rightElement = right[j];
  23. if (i < leftLength && (j >= rightLength || compare(leftElement, rightElement, userDefinedObject) <= 0)) {
  24. array[k] = leftElement;
  25. ++i;
  26. } else if (j < rightLength) {
  27. array[k] = rightElement;
  28. ++j;
  29. }
  30. }
  31. }
  32. function sort(array, compare, userDefinedObject, start, end) {
  33. if (start >= end) {
  34. return;
  35. }
  36. var middle = Math.floor((start + end) * 0.5);
  37. sort(array, compare, userDefinedObject, start, middle);
  38. sort(array, compare, userDefinedObject, middle + 1, end);
  39. merge(array, compare, userDefinedObject, start, middle, end);
  40. }
  41. /**
  42. * A stable merge sort.
  43. *
  44. * @exports mergeSort
  45. * @param {Array} array The array to sort.
  46. * @param {mergeSort~Comparator} comparator The function to use to compare elements in the array.
  47. * @param {*} [userDefinedObject] Any item to pass as the third parameter to <code>comparator</code>.
  48. *
  49. * @example
  50. * // Assume array contains BoundingSpheres in world coordinates.
  51. * // Sort them in ascending order of distance from the camera.
  52. * var position = camera.positionWC;
  53. * Cesium.mergeSort(array, function(a, b, position) {
  54. * return Cesium.BoundingSphere.distanceSquaredTo(b, position) - Cesium.BoundingSphere.distanceSquaredTo(a, position);
  55. * }, position);
  56. */
  57. function mergeSort(array, comparator, userDefinedObject) {
  58. //>>includeStart('debug', pragmas.debug);
  59. if (!defined(array)) {
  60. throw new DeveloperError('array is required.');
  61. }
  62. if (!defined(comparator)) {
  63. throw new DeveloperError('comparator is required.');
  64. }
  65. //>>includeEnd('debug');
  66. var length = array.length;
  67. var scratchLength = Math.ceil(length * 0.5);
  68. // preallocate space in scratch arrays
  69. leftScratchArray.length = scratchLength;
  70. rightScratchArray.length = scratchLength;
  71. sort(array, comparator, userDefinedObject, 0, length - 1);
  72. // trim scratch arrays
  73. leftScratchArray.length = 0;
  74. rightScratchArray.length = 0;
  75. }
  76. /**
  77. * A function used to compare two items while performing a merge sort.
  78. * @callback mergeSort~Comparator
  79. *
  80. * @param {*} a An item in the array.
  81. * @param {*} b An item in the array.
  82. * @param {*} [userDefinedObject] An object that was passed to {@link mergeSort}.
  83. * @returns {Number} Returns a negative value if <code>a</code> is less than <code>b</code>,
  84. * a positive value if <code>a</code> is greater than <code>b</code>, or
  85. * 0 if <code>a</code> is equal to <code>b</code>.
  86. *
  87. * @example
  88. * function compareNumbers(a, b, userDefinedObject) {
  89. * return a - b;
  90. * }
  91. */
  92. export default mergeSort;