algorithms
time complexity
counting sort
sorting algorithms
computer science

The time complexity of counting sort

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

Counting sort is an efficient sorting algorithm for dealing with a collection of items that have keys lying in a well-defined range. Unlike comparison-based algorithms, counting sort's performance is not limited by the number of elements to be sorted, but rather by the range of the keys. It is an integer sorting algorithm that is particularly notable for its linear time complexity under certain conditions.

Overview of Counting Sort

Counting sort operates on an array of integer keys, transforming them into a position-based index map to organize the items directly. It requires extra space for counting the occurrences of each distinct key. The main idea is to count the occurrence of each key, aggregate the counts to determine the position of each key in the output sequence, and finally, compile the sorted array.

Steps of Counting Sort:

  1. Count the Occurrences: Create a count array that holds the number of times each key appears. The size of this array should be equal to the range of the keys.
  2. Calculate Cumulative Counts: Transform the count array into a cumulative count array by iterating through and adding the count of each key to the count of the previous key.
  3. Place Elements in Output: Using the cumulative count array, place each object from the input list into its correct position in the output array. Decrease the count in the cumulative array as items are placed.
  4. Copy Back to Original Array: If needed, the sorted array can be copied back into the original array.

Example:

Consider an array of integers `[4, 2, 2, 8, 3, 3, 1]`.

Initial Count Array: Initialize an array `count` of size `max_value + 1` (where `max_value` is the largest number in the input array), all elements set to zero.

012345678
000000000

Count Occurrences: Update the count array with the frequency of each element.

012345678
012210001

Calculate Cumulative Counts:

012345678
013566667

Place Elements in Output Array:

Output
[1, 2, 2, 3, 3, 4, 8]

Time Complexity of Counting Sort

Complexity Analysis

The time complexity of counting sort depends on the size of the input array (`n`) and the range of the input (`k`).

  1. Counting Occurrences: This step runs in O(n)O(n) time since each element from the input array needs to be examined and counted once.
  2. Cumulative Counts: This involves a single pass through the `count` array, taking O(k)O(k) time.
  3. Building the Output: This takes O(n)O(n) time, as you are placing each element in the output array based on the indices in your cumulative count array.

Combining these parts, the time complexity of counting sort is O(n+k)O(n + k).

Practical Considerations

• The algorithm is optimal when the range of the numbers (k) is not significantly larger than the number of elements (n). Specifically, counting sort performs efficiently when k=O(n)k = O(n), leading to a linear time complexity, i.e., O(n)O(n). • The space complexity of counting sort is O(k+n)O(k + n), due to the `count` array and the output array.

Key Points Summary

ParameterDescription
Input RequirementsWorks with non-negative integers or numbers with known range.
Time ComplexityO(n+k)O(n + k) where n is the number of elements and k is the range of input.
Space ComplexityO(k+n)O(k + n)
Stable SortYes
ParallelizableYes
Best Use CaseSorting integers with known small range

Conclusion

Counting sort is a highly efficient, stable, and non-comparison sorting algorithm when applied under appropriate conditions (i.e., small range relative to input size). It is especially suited for applications involving integers or discrete range data, such as sorting scores in exams, organizing frequency data, or multi-key sorting in conjunction with other sorting algorithms. Counting sort manages to sidestep the traditional lower bound of comparison sorts, achieving linear time sorting through direct use of input keys to steer the sorting process.


Related reading
Course
Intermediate
27 lessons
15 hours
DSA Fundamentals

Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Data Structures & Algorithms practice on Codemia

Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.

Practice algorithms

All Rights Reserved.