Class HeapSort<T>

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:

  1. Building a max heap from the input data
  2. Repeatedly extracting the maximum element from the heap and placing it at the end
  3. Reducing the heap size and re-heapifying

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

const heapSort = new HeapSort([3, 4, 1]);
heapSort.sort(); // returns new array with [1, 3, 4]
// or
HeapSort.sort([3, 4, 1]); // returns new array with [1, 3, 4]

Example

const heapSort = new HeapSort(
['alice', 'bob', 'charlie', 'alex', 'gavin'],
(a : string, b : string) => a.localeCompare(b)
);
heapSort.sort(); // returns new array with ['alex', 'alice', 'bob', 'charlie', 'gavin']

Type Parameters

  • T

Hierarchy

  • HeapSort

Constructors

  • Type Parameters

    • T

    Parameters

    • array: T[] = []
    • comparatorFn: ((a, b) => number) = HeapSort.defaultComparator
        • (a, b): number
        • Default comparator function used for sorting

          Parameters

          • a: any

            first element to compare

          • b: any

            second element to compare

          Returns number

          a negative number if a < b, 0 if a = b, a positive number if a > b

    Returns HeapSort<T>

Properties

defaultComparator: ((a, b) => number)

Type declaration

    • (a, b): number
    • Parameters

      • a: T
      • b: T

      Returns number

size: number
values: T[]

Methods

  • Heapify a subtree rooted at index i (instance method)

    Parameters

    • array: T[]

      the array to heapify

    • heapSize: number

      size of the heap

    • rootIndex: number

      index of the root of the subtree

    Returns void

  • Sorts the array using the HeapSort algorithm.

    Returns T[]

    a new array with the elements sorted

    Example

    const heapSort = new HeapSort([3, 4, 1]);
    heapSort.sort(); // returns new array with [1, 3, 4]
  • Default comparator function used for sorting

    Parameters

    • a: any

      first element to compare

    • b: any

      second element to compare

    Returns number

    a negative number if a < b, 0 if a = b, a positive number if a > b

  • Heapify a subtree rooted at index i

    Parameters

    • array: any[]

      the array to heapify

    • heapSize: number

      size of the heap

    • rootIndex: number

      index of the root of the subtree

    • comparatorFn: ((a, b) => number)

      comparator function for comparison

        • (a, b): number
        • Parameters

          • a: any
          • b: any

          Returns number

    Returns void

  • Sorts the array using the HeapSort algorithm. Static method, no need to instantiate class

    Parameters

    • array: any[]

      the array to be sorted

    • comparatorFn: ((a, b) => number) = ...

      the comparator function used for sorting, defaults to numerical comparison

        • (a, b): number
        • Default comparator function used for sorting

          Parameters

          • a: any

            first element to compare

          • b: any

            second element to compare

          Returns number

          a negative number if a < b, 0 if a = b, a positive number if a > b

    Returns any[]

    a new array with the elements sorted

Generated using TypeDoc