Sort elements by counting the frequency of their appearance
How It Works: Link to Video
- Worst Case Complexity: O(n+k) If we want to sort in ascending order and the array is in descending order then, the worst case occurs.
- Best Case Complexity: O(n+k) It occurs when the array is already sorted
- Average Case Complexity: O(n+k) It occurs when the elements of the array are in jumbled order (neither ascending nor descending).
- The space complexity of Counting Sort is O(max). Larger the range of elements, larger is the space complexity.
- Frequency Based
- Stable
- Not Inplace
- Consumes more memory
- There are smaller integers with multiple counts.
- Linear complexity is the need.
- Counting sort generally performs faster than all comparison-based sorting algorithms, such as merge sort and quicksort, if the range of input is of the order of the number of input.
- Counting sort is easy to code
- Counting sort is a stable algorithm.
- Counting sort does’t work on decimal values.
- Counting sort is inefficient if the range of values to be sorted is very large.
- Counting sort is not an In-place sorting algorithm, It uses extra space for sorting the array elements.