Default comparator function used for sorting
first element to compare
second element to compare
a negative number if a < b, 0 if a = b, a positive number if a > b
Private Readonly defaultPrivate Readonly sizePrivate Readonly valuesPrivate mergeStatic defaultStatic Private mergeMerges two sorted arrays into a single sorted array. Static helper method used by the sort algorithm.
the left sorted array
the right sorted array
the comparator function used for sorting
a new array with elements from both arrays sorted
Static sortSorts the array using the MergeSort algorithm. Static method, no need to instantiate class
the array to be sorted
the comparator function used for sorting, defaults to numerical comparison
Default comparator function used for sorting
first element to compare
second element to compare
a negative number if a < b, 0 if a = b, a positive number if a > b
a new array with the elements sorted
Generated using TypeDoc
MergeSort is a divide-and-conquer sorting algorithm that divides the input array into two halves, recursively sorts them, and then merges the two sorted halves. It is one of the most efficient sorting algorithms with a time complexity of O(n log n) in all cases.
The algorithm works by repeatedly dividing the array in half until each sub-array contains a single element (which is inherently sorted), and then merging those sorted sub-arrays back together. The merge operation compares elements from both sub-arrays and places them in the correct order.
MergeSort is stable (maintains relative order of equal elements) and has a space complexity of O(n) due to the auxiliary arrays needed for merging.
The time complexity is O(n log n) for best, average, and worst cases. The space complexity is O(n) for the auxiliary arrays.
Example
Example