Skip to content

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.

  1. Builds a max-heap from the array: array[i] >= array[2i+1] and array[i] >= array[2i+2], so the largest element is at index 0.
  2. 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: bestTime: averageTime: worstMemoryStableIn place
O(n log n)O(n log n)O(n log n)O(1)noyes

Type Parameters ​

Type ParameterDescription
T

Parameters ​

ParameterTypeDefault valueDescription
arrayT[]undefinedArray to sort. It is modified.
compareFn?CompareFn<T>defaultCompareOrder of elements, ascending by default.

Returns ​

void

Nothing — array itself is sorted.

MIT License