C++ Program for Counting Sort with Example and Explanation
void countingSort(int arr[], int n) { int max = *max_element(arr, arr + n); int count[max + 1] = {0}; for (int i = 0; i < n; i++) count[arr[i]]++; int index = 0; for (int i = 0; i <= max; i++) while (count[i]--) arr[index++] = i; } sorts the array in ascending order.Examples
How to Think About It
Algorithm
Code
#include <iostream> #include <algorithm> using namespace std; void countingSort(int arr[], int n) { int max = *max_element(arr, arr + n); int count[max + 1] = {0}; for (int i = 0; i < n; i++) count[arr[i]]++; int index = 0; for (int i = 0; i <= max; i++) { while (count[i] > 0) { arr[index++] = i; count[i]--; } } } int main() { int arr[] = {4, 2, 2, 8, 3, 3, 1}; int n = sizeof(arr) / sizeof(arr[0]); countingSort(arr, n); cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
Dry Run
Let's trace the array {4, 2, 2, 8, 3, 3, 1} through the counting sort code.
Find max value
Maximum value in array is 8.
Initialize count array
Create count array of size 9 (0 to 8) with all zeros.
Count occurrences
Count array after counting: [0,1,2,2,1,0,0,0,1]
Rebuild sorted array
Write numbers back: 1 once, 2 twice, 3 twice, 4 once, 8 once.
Final sorted array
Sorted array is {1, 2, 2, 3, 3, 4, 8}.
| Number | Count |
|---|---|
| 0 | 0 |
| 1 | 1 |
| 2 | 2 |
| 3 | 2 |
| 4 | 1 |
| 5 | 0 |
| 6 | 0 |
| 7 | 0 |
| 8 | 1 |
Why This Works
Step 1: Counting occurrences
The code uses a count array where each index represents a number, and the value at that index is how many times it appears in the input.
Step 2: Rebuilding sorted array
By iterating over the count array and writing each number as many times as counted, the original array becomes sorted.
Step 3: No comparisons needed
Counting sort does not compare elements but uses counting, making it very fast for numbers in a small range.
Alternative Approaches
#include <iostream> #include <vector> #include <algorithm> using namespace std; void countingSort(vector<int>& arr) { int max = *max_element(arr.begin(), arr.end()); vector<int> count(max + 1, 0); for (int num : arr) count[num]++; int index = 0; for (int i = 0; i <= max; i++) { while (count[i]-- > 0) { arr[index++] = i; } } } int main() { vector<int> arr = {4, 2, 2, 8, 3, 3, 1}; countingSort(arr); cout << "Sorted array: "; for (int num : arr) cout << num << " "; cout << endl; return 0; }
#include <iostream> #include <algorithm> using namespace std; void countingSort(int arr[], int n) { int max = *max_element(arr, arr + n); int count[max + 1] = {0}; int output[n]; for (int i = 0; i < n; i++) count[arr[i]]++; for (int i = 1; i <= max; i++) count[i] += count[i - 1]; for (int i = n - 1; i >= 0; i--) { output[count[arr[i]] - 1] = arr[i]; count[arr[i]]--; } for (int i = 0; i < n; i++) arr[i] = output[i]; } int main() { int arr[] = {4, 2, 2, 8, 3, 3, 1}; int n = sizeof(arr) / sizeof(arr[0]); countingSort(arr, n); cout << "Sorted array: "; for (int i = 0; i < n; i++) cout << arr[i] << " "; cout << endl; return 0; }
Complexity: O(n + k) time, O(k) space
Time Complexity
Counting sort runs in O(n + k) where n is the number of elements and k is the range of input values, because it counts occurrences and then reconstructs the array.
Space Complexity
It uses O(k) extra space for the count array, which depends on the maximum value in the input.
Which Approach is Fastest?
Counting sort is fastest when the range k is not much larger than n; otherwise, comparison-based sorts may be better.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Basic counting array | O(n + k) | O(k) | Small range integers |
| Vector-based count array | O(n + k) | O(k) | Dynamic or unknown range |
| Stable counting sort with output array | O(n + k) | O(n + k) | When stability is needed |