• Counting Sort counts the number of occurrences of each value in the array and then uses these counts to compute the position of each element in the sorted array.

    Time Complexity The time complexity of the counting sort algorithm is O(n+k), where:

    • n is the number of elements in the input array
    • k is the range of input values (difference between the maximum and minimum values)

    Space Complexity The space complexity of the counting sort algorithm is O(k), as it requires additional space to store the count of each value in the range. Note that counting sort is efficient when k is not significantly larger than n. If k is much larger than n, the counting sort can be less efficient compared to other sorting algorithms like quicksort or mergesort which have a time complexity of O(n log n).

    Parameters

    • arr: number[]

      array of integers, which should be sorted

    Returns number[]

    sorted array

Generated using TypeDoc