@ts-standard-library/algorithms
Version:
A collection of algorithms for TypeScript.
21 lines (20 loc) • 843 B
TypeScript
/**
* Sorts an array of non-negative integers using the counting sort algorithm.
*
* @param array - The array of non-negative integers to sort.
* @param max - The maximum value in the array.
* @returns A new array containing the sorted elements.
*
* @remarks
* Counting sort is efficient for sorting integers when the range of input values (max) is not significantly larger than the number of elements.
* This implementation assumes all numbers in the input array are between 0 and `max`, inclusive.
*
* @example
* ```typescript
* const arr = [4, 2, 2, 8, 3, 3, 1];
* const sorted = countingSort(arr, 8);
* // sorted: [1, 2, 2, 3, 3, 4, 8]
* ```
* @see {@link https://en.wikipedia.org/wiki/Counting_sort} for more information on counting sort.
*/
export declare function countingSort(array: number[], max: number): number[];