Class InsertionSort<T>

InsertionSort is a simple sorting algorithm that builds the final sorted array one item at a time. It is much less efficient on large lists than more advanced algorithms such as quicksort, heapsort, or merge sort. However, insertion sort provides several advantages:

  • Simple implementation
  • Efficient for small data sets
  • Adaptive: efficient for data sets that are already substantially sorted
  • Stable: does not change the relative order of elements with equal keys
  • In-place: only requires a constant amount O(1) of additional memory space

The algorithm iterates through the input array, growing the sorted array behind it. At each iteration, it removes one element from the input data, finds the location it belongs within the sorted list, and inserts it there. It repeats until no input elements remain.

The worst-case time complexity is O(n^2), the average-case time complexity is O(n^2), and the best-case time complexity is O(n) when the array is already sorted.

Example

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

Example

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

Type Parameters

  • T

Hierarchy

  • InsertionSort

Constructors

  • Type Parameters

    • T

    Parameters

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

Properties

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

Type declaration

    • (a, b): number
    • Parameters

      • a: T
      • b: T

      Returns number

size: number
values: T[]

Methods

  • Sorts the array using the InsertionSort algorithm.

    Returns T[]

    a new array with the elements sorted

    Example

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

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