ts-ds / heapSort
Function: heapSort()
ts
function heapSort<T>(array, compareFn?): void;Defined in: sort/heap/heap.ts:19
Sorts an array in place using heap sort.
- Builds a max-heap from the array:
array[i] >= array[2i+1]andarray[i] >= array[2i+2], so the largest element is at index 0. - Moves the largest element to the end, shrinks the heap by one and restores the heap property with
siftDown. Repeats until the heap is empty.
| Time: best | Time: average | Time: worst | Memory | Stable | In place |
|---|---|---|---|---|---|
| O(n log n) | O(n log n) | O(n log n) | O(1) | no | yes |
Type Parameters
| Type Parameter | Description |
|---|---|
T |
Parameters
| Parameter | Type | Default value | Description |
|---|---|---|---|
array | T[] | undefined | Array to sort. It is modified. |
compareFn? | CompareFn<T> | defaultCompare | Order of elements, ascending by default. |
Returns
void
Nothing — array itself is sorted.