Class SelectionSort<T>

SelectionSort is a simple sorting algorithm that divides the input array into a sorted and an unsorted region. It repeatedly selects the smallest (or largest) element from the unsorted region and moves it to the end of the sorted region.

The algorithm maintains two subarrays:

  1. The subarray which is already sorted
  2. The remaining subarray which is unsorted

In every iteration, the minimum element from the unsorted subarray is picked and moved to the sorted subarray.

The time complexity is O(n^2) in all cases (best, average, and worst), making it inefficient for large datasets. However, it performs well on small lists and requires minimal memory writes compared to other sorting algorithms.

Example

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

Example

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

Type Parameters

  • T

Hierarchy

  • SelectionSort

Constructors

  • Type Parameters

    • T

    Parameters

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

    Returns T[]

    a new array with the elements sorted

    Example

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