Quick select

Implementation of a selection algorithm on an unsorted array related to the quicksort sorting algorithm which finds the nth smallest element in O(n) average time.

quickSelect()

/**
 * Selects the nth smallest item in unsorted array. Throws an error if array is empty or
 * n value is out of the array bounds.
 *
 * @param arr Array to select from.
 * @param n Sequential number.
 * @param compare Comparison function. Items are compared as numbers by default.
 * @returns Selected item.
 */
export function quickSelect<T>(arr: T[], n: number = 0, compare: CompareFunc<T> = compareAsNumbers): T;

Selects the nth smallest item in unsorted array. Throws an error if array is empty or n value is out of the array bounds.

Last updated

Was this helpful?