Seth.

Counting Sort

Counting Sort - Technical Overview

1. Algorithm Description: Counting Sort is an integer sorting algorithm that counts the occurrences of each distinct element in the input, then calculates their position in the sorted array.

2. Steps of Counting Sort:

  • Find the maximum value in the array to create the count array.
  • Count the occurrences of each element in the input array.
  • Use the count array to place elements in the correct order in the output array.
  • Iterate over the count array to build the sorted array.

3. Time Complexity: O(n + k), where n is the number of elements in the input array, and k is the range of the input values.

4. Space Complexity: O(n + k), where additional space is required for the count array.

5. Stability: Counting Sort is a stable sorting algorithm.

6. Limitations: Counting Sort is effective when the range of input elements (k) is not significantly larger than the number of elements (n). Otherwise, it may require large amounts of memory.