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 heapifyStatic defaultStatic Private heapifyHeapify a subtree rooted at index i
the array to heapify
size of the heap
index of the root of the subtree
comparator function for comparison
Static sortSorts the array using the HeapSort 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
HeapSort is a comparison-based sorting algorithm that uses a binary heap data structure. It divides its input into a sorted and an unsorted region, and iteratively shrinks the unsorted region by extracting the largest element and moving it to the sorted region.
The algorithm works by:
HeapSort is an in-place algorithm (though this implementation returns a new array for consistency with other sorting algorithms in this library). It has a time complexity of O(n log n) in all cases (best, average, and worst), making it efficient for large datasets.
Unlike QuickSort, HeapSort's worst-case time complexity is O(n log n), but it generally performs slower than QuickSort in practice due to poor cache locality.
Example
Example