Back to Data Structures & Algorithms
2026-02-055 min read

Counting Sort (Data Structures & Algorithms)

Learn Counting Sort (Data Structures & Algorithms) step by step with clear examples and exercises.

Why This Matters

Counting sort is a simple and efficient sorting algorithm that works well for inputs with small unique elements. You'll learn about counting sort, its implementation in Python, C++, and Java, and common mistakes to avoid when using it.

Why This Matters

Counting sort is useful when dealing with large datasets where the range of input values is not too large. It has a linear time complexity of O(n+k), where n is the number of elements to be sorted and k is the maximum value among the input elements. This makes it faster than other comparison-based sorting algorithms like quicksort, mergesort, or heapsort when the range of input values is small.

Counting sort is also useful in competitive programming contests and interviews as a tool to solve problems efficiently. It can help you avoid common pitfalls when dealing with large datasets and improve your problem-solving skills.

Prerequisites

To understand counting sort, you should be familiar with the following concepts:

  1. Basic Python, C++, or Java programming
  2. Array data structure
  3. Time complexity analysis
  4. Familiarity with other sorting algorithms like quicksort, mergesort, and heapsort

Core Concept

Counting sort works by counting the frequency of each element in the input array and then creating a new output array based on those counts. Here's a high-level overview of the algorithm:

  1. Create an auxiliary array count of size equal to the maximum possible value among the input elements. Initialize all elements to zero.
  2. Iterate through the input array and increment the corresponding element in the count array for each encountered element.
  3. Create another auxiliary array cumulative_count to store the cumulative count of elements up to a given index. This can be computed by iterating through the count array and summing up the values.
  4. Initialize an output array of the same size as the input array, with all elements set to zero.
  5. Iterate through the cumulative_count array and place each element in the output array at the appropriate position based on its cumulative count index.
  6. Return the sorted output array.

Worked Example

Let's consider a simple example to illustrate counting sort:

def counting_sort(arr, max_value):
n = len(arr)
count = [0] * (max_value + 1)
cumulative_count = [0] * (max_value + 1)
output = [0] * n

Step 1: Count the frequency of each element in the input array

for i in range(n):

count[arr[i]] += 1

Step 2: Compute cumulative counts

for i in range(1, max_value + 1):

cumulative_count[i] = cumulative_count[i - 1] + count[i]

Step 3: Place each element in the output array at the appropriate position

for i in range(n - 1, -1, -1):

output[cumulative_count[arr[i]] - 1] = arr[i]

cumulative_count[arr[i]] -= 1

return output


In this example, we define a `counting_sort` function that takes an input array and the maximum value among its elements as arguments. The function performs the six steps described in the core concept section to sort the input array using counting sort.

Common Mistakes

  1. Not handling negative numbers: Counting sort does not work for arrays containing negative numbers, as it requires a fixed maximum value. To handle such cases, you can modify the algorithm to use two auxiliary arrays: one for positive numbers and another for negative numbers.
  2. Incorrect initialization of cumulative_count array: Make sure to initialize all elements of the cumulative_count array to zero, as it is used to store the cumulative count of elements up to a given index.
  3. Misunderstanding the role of the auxiliary arrays: The count and cumulative_count arrays are crucial in counting sort, so make sure you understand their purpose and how they help in sorting the input array.
  4. Not returning the sorted output array: After placing each element in the output array at the appropriate position, don't forget to return it as the final sorted array.

Practice Questions

  1. Implement counting sort in C++ using STL vectors for the auxiliary arrays.
  2. Modify the counting_sort function to handle negative numbers by using two separate auxiliary arrays for positive and negative numbers.
  3. Write a Java implementation of counting sort that sorts an array of integers containing duplicate elements.
  4. Given an array of strings, implement counting sort with a custom comparator that sorts the strings lexicographically.
  5. Implement counting sort in Python using dictionaries for the auxiliary arrays.

FAQ

  1. Why is counting sort faster than other comparison-based sorting algorithms when the range of input values is small?

Counting sort has a linear time complexity (O(n+k)) because it sorts elements based on their frequency, not by comparing them. When the range of input values is small, the constant term in the big O notation becomes insignificant compared to n, making counting sort faster than other comparison-based sorting algorithms like quicksort or mergesort.

  1. Can counting sort be used for large datasets?

Counting sort can handle large datasets if the range of input values is not too large. However, when the range is very large, other sorting algorithms like mergesort or heapsort may perform better due to their adaptive nature and ability to handle larger ranges efficiently.

  1. What are some applications of counting sort?

Counting sort can be used in various fields, including data compression, network routing, and DNA sequencing. It is also a useful tool for competitive programming contests and interviews as it helps solve problems efficiently.

  1. Can counting sort be used to sort floating-point numbers?

Counting sort works only with integer values because it relies on the frequency of each element in the input array. To sort floating-point numbers, you can use other sorting algorithms like quicksort or mergesort that are designed to handle real numbers.

  1. Is counting sort a stable sort?

Counting sort is not a stable sort because it may change the relative order of equal elements during the placement step. If stability is important for your application, you should use a stable sorting algorithm like mergesort or timsort instead.

Counting Sort (Data Structures & Algorithms) | Data Structures & Algorithms | XQA Learn