Class MergeSort<T>

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

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

Example

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

Type Parameters

  • T

Hierarchy

  • MergeSort

Constructors

  • Type Parameters

    • T

    Parameters

    • array: T[] = []
    • comparatorFn: ((a, b) => number) = MergeSort.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 MergeSort<T>

Properties

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

Type declaration

    • (a, b): number
    • Parameters

      • a: T
      • b: T

      Returns number

size: number
values: T[]

Methods

  • Instance helper method to merge two sorted arrays.

    Parameters

    • left: T[]

      the left sorted array

    • right: T[]

      the right sorted array

    Returns T[]

    a new array with elements from both arrays sorted

  • Sorts the array using the MergeSort algorithm.

    Returns T[]

    a new array with the elements sorted

    Example

    const mergeSort = new MergeSort([3, 4, 1]);
    mergeSort.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

  • Merges two sorted arrays into a single sorted array. Static helper method used by the sort algorithm.

    Parameters

    • left: any[]

      the left sorted array

    • right: any[]

      the right sorted array

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

      the comparator function used for sorting

        • (a, b): number
        • Parameters

          • a: any
          • b: any

          Returns number

    Returns any[]

    a new array with elements from both arrays sorted

  • Sorts the array using the MergeSort 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