Counting Sort: An Efficient Algorithm for Specific Scenarios
Counting Sort is a non-comparison-based sorting algorithm. It is highly efficient for sorting elements within a specific range, making it a valuable tool in competitive programming and computer science curricula like GATE. Unlike comparison sorts (like Merge Sort or Quick Sort) that rely on comparing elements, Counting Sort leverages the value of the elements themselves to determine their sorted positions.
How Counting Sort Works
The core idea behind Counting Sort is to count the occurrences of each distinct element in the input array and then use these counts to determine the positions of the elements in the sorted output array. It's particularly effective when the range of input values (k) is not significantly larger than the number of elements (n).
Counting Sort uses auxiliary space to count element frequencies and then place them in their correct sorted positions.
It involves creating a 'count' array to store the frequency of each element and then using this count array to build the sorted output. This avoids direct element comparisons.
The algorithm typically proceeds in three main steps:
- Create a Count Array: An auxiliary array, often called
count
, is created with a size equal to the range of input values (maximum value + 1). This array is initialized to zeros. Iterate through the input array and for each elementx
, incrementcount[x]
. - Modify the Count Array: Transform the
count
array such thatcount[i]
now contains the actual position of the elementi
in the output array. This is achieved by cumulatively summing the counts:count[i] = count[i] + count[i-1]
for alli
from 1 to the maximum value. - Build the Output Array: Create an output array of the same size as the input array. Iterate through the input array in reverse order. For each element
x
, place it atoutput[count[x] - 1]
and then decrementcount[x]
. Iterating in reverse ensures stability (elements with the same value maintain their relative order).
Its time complexity is linear (O(n+k)) when the range of input values (k) is not significantly larger than the number of elements (n), whereas comparison sorts are typically O(n log n).
Example Walkthrough
Let's sort the array
[4, 2, 2, 8, 3, 3, 1]
Consider the input array: [4, 2, 2, 8, 3, 3, 1]
. The maximum element is 8.
Step 1: Count Array Creation
Initialize count
array of size 9 (0 to 8) with zeros: [0, 0, 0, 0, 0, 0, 0, 0, 0]
.
Iterate through input array:
1
:count[1]
becomes 1.2
:count[2]
becomes 1.2
:count[2]
becomes 2.3
:count[3]
becomes 1.3
:count[3]
becomes 2.4
:count[4]
becomes 1.8
:count[8]
becomes 1. Resultingcount
array:[0, 1, 2, 2, 1, 0, 0, 0, 1]
(Indices represent values 0-8).
Step 2: Modify Count Array (Cumulative Sum)
count[0] = 0
count[1] = count[1] + count[0] = 1 + 0 = 1
count[2] = count[2] + count[1] = 2 + 1 = 3
count[3] = count[3] + count[2] = 2 + 3 = 5
count[4] = count[4] + count[3] = 1 + 5 = 6
count[5] = count[5] + count[4] = 0 + 6 = 6
count[6] = count[6] + count[5] = 0 + 6 = 6
count[7] = count[7] + count[6] = 0 + 6 = 6
count[8] = count[8] + count[7] = 1 + 6 = 7
Modified count
array: [0, 1, 3, 5, 6, 6, 6, 6, 7]
.
Step 3: Build Output Array
Initialize output
array of size 7: [_, _, _, _, _, _, _]
.
Iterate input [4, 2, 2, 8, 3, 3, 1]
in reverse:
- Element
1
:count[1]
is 1. Place1
atoutput[1-1] = output[0]
.output = [1, _, _, _, _, _, _]
. Decrementcount[1]
to 0. - Element
3
:count[3]
is 5. Place3
atoutput[5-1] = output[4]
.output = [1, _, _, _, 3, _, _]
. Decrementcount[3]
to 4. - Element
3
:count[3]
is 4. Place3
atoutput[4-1] = output[3]
.output = [1, _, _, 3, 3, _, _]
. Decrementcount[3]
to 3. - Element
8
:count[8]
is 7. Place8
atoutput[7-1] = output[6]
.output = [1, _, _, 3, 3, _, 8]
. Decrementcount[8]
to 6. - Element
2
:count[2]
is 3. Place2
atoutput[3-1] = output[2]
.output = [1, _, 2, 3, 3, _, 8]
. Decrementcount[2]
to 2. - Element
2
:count[2]
is 2. Place2
atoutput[2-1] = output[1]
.output = [1, 2, 2, 3, 3, _, 8]
. Decrementcount[2]
to 1. - Element
4
:count[4]
is 6. Place4
atoutput[6-1] = output[5]
.output = [1, 2, 2, 3, 3, 4, 8]
. Decrementcount[4]
to 5.
Final sorted array: [1, 2, 2, 3, 3, 4, 8]
.
Text-based content
Library pages focus on text content
Time and Space Complexity
Aspect | Counting Sort | Comparison Sorts (e.g., Merge Sort) |
---|---|---|
Time Complexity (Best/Average/Worst) | O(n + k) | O(n log n) |
Space Complexity | O(n + k) | O(n) or O(log n) depending on implementation |
Counting Sort is a linear time sorting algorithm, but it's only practical when the range of input values (k) is not excessively large compared to the number of elements (n). If k is much larger than n, its space and time complexity can become prohibitive.
When to Use Counting Sort
Counting Sort is ideal for scenarios where:
- The input consists of integers.
- The range of input values (k) is known and relatively small.
- Stability is required (maintaining the relative order of equal elements).
Common applications include sorting student scores, character frequencies, or data within a limited numerical range. It's also a foundational algorithm for Radix Sort.
It is not suitable for sorting elements with a very large range of values or non-integer data types.
Learning Resources
A comprehensive explanation of the Counting Sort algorithm with detailed steps, pseudocode, and complexity analysis.
Provides a clear overview of Counting Sort, its working principle, and its time and space complexity.
A visual and step-by-step explanation of how Counting Sort works, making the concept easier to grasp.
Offers a concise explanation and implementation of Counting Sort in various programming languages.
Part of a university-level algorithms course, this lecture covers Counting Sort and its relation to Radix Sort.
An excerpt from a renowned algorithms textbook, detailing the theoretical underpinnings and correctness of Counting Sort.
A broad overview of the Counting Sort algorithm, its history, variations, and applications.
A lecture from a popular online course that explains Counting Sort and its efficiency.
A practical guide to implementing Counting Sort in Python, with code examples and explanations.
The official syllabus for GATE Computer Science, which includes Sorting Algorithms as a key topic.