QuickSort is a method of sorting an array by recursively dividing the array into smaller subarrays. It is a divide-and-conquer algorithm that selects a 'pivot' element from the array and partitions the other elements into two sub-arrays according to whether they are less than or greater than the pivot. The sub-arrays are then sorted recursively. The base case of the recursion is an array of zero or one element, which is guaranteed to be sorted. The pivot selection and partitioning steps can be done in different ways and the efficiency of the algorithm depends on the choice of these steps.

The worst-case time complexity of QuickSort is O(n^2), but the average-case time complexity is O(n log n) and the best-case time complexity is O(n log n).

Example

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

Example

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

Hierarchy

  • QuickSort

Constructors

  • Parameters

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

Properties

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

Type declaration

    • (a, b): number
    • Parameters

      • a: any
      • b: any

      Returns number

size: number
values: any[]

Methods

  • Sorts the array using the QuickSort algorithm.

    Returns any[]

    a new array with the elements sorted

    Example

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