Counting Sort in C++
What is counting sort in C++?
Counting sort is a sorting algorithm that sorts the elements in a logical order, of an array by counting the number of time of each unique element is present in the array. Then using the count of each unique element we can sort them in a stored order in another array.
It is used as a sub-routine to another sorting algorithm like radix sort. In this article, we will learn more about counting sort in C++.

What is counting sort in C++?
Counting sort is a sorting algorithm that sorts the elements in a logical order, of an array by counting the number of time of each unique element is present in the array. Then using the count of each unique element we can sort them in a stored order in another array. It is used as a sub-routine to another sorting algorithm like radix sort. In this article, we will learn more about counting sort in C++.
Working of counting sort in C++
To sort any list into a logical order using counting sort following steps are followed :-
- Create a count array with value of every index equal to zero of size one greater then the maximum element in the array.
- Now store the count of each unique element at their respective index.
- Sorting of array is done with the help of total count, so store the total or cumulative sum of the elements of the count array.
- Place each element from the array in position equal to its count-1.
- Decrease the count of element placed by 1.

Algorithm for counting sort in C++
Counting Sort (array P, array Q, int k)
- For i ← 1 to k
- do C [i] ← 0 [ θ(k) times]
- for j ← 1 to length [A]
- do C[A[j]] ← C [A [j]]+1 [θ(n) times]
// C [i] now contain the number of elements equal to i - for i ← 2 to k
- do C [i] ← C [i] + C[i-1] [θ(k) times]
//C[i] now contain the number of elements ≤ i - for j ← length [A] down to 1 [θ(n) times]
- do B[C[A[j] ← A [j]
- C[A[j] ← C[A[j]-1
Program for counting sort in C++
// Counting sort in C++ programming #include<iostream> #define MAX 255 using namespace std; void countSort(int array[], int size) { int output[MAX]; int count[MAX]; int max = array[0]; // Here we find the largest item in the array for (int i = 1; i < size; i++) { if (array[i] > max) max = array[i]; } // Initialize the count for each element in array to 0 for (int i = 0; i <= max; ++i) { count[i] = 0; } // For each element we store the count for (int i = 0; i < size; i++) { count[array[i]]++; } // Store the cummulative count of each array for (int i = 1; i <= max; i++) { count[i] += count[i - 1]; } // Search the index of each element of the actual array in count array, and // place the elements in output array for (int i = size - 1; i >= 0; i--) { output[count[array[i]] - 1] = array[i]; count[array[i]]--; } // Transfer the sorted items into actual array for (int i = 0; i < size; i++) { array[i] = output[i]; } } // Function to print an array void display(int array[], int size) { for (int i = 0; i < size; i++) cout << array[i] << " "; cout << endl; } // Driver code int main() { int array[] = {2, 5, 2, 8, 1, 4, 1}; int n = sizeof(array) / sizeof(array[0]); countSort(array, n); display(array, n); return 0; }
Output
1 1 2 2 3 4 5 8
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Time and Space Complexity :
Case | Time Complexity | Space Complexity |
---|---|---|
Best Case | O(n + k) | O(n + k) |
Average Case | O(n + k) | O(n + k) |
Worst Case | O(n + k) | O(n + k) |
Explanation :
- The program starts by finding the maximum element in the array to determine the range of input values.
- A count array is initialized with all zeros to store the frequency of each element.
- Each element’s occurrence is counted and stored in the count array based on its value.
- The count array is then updated to hold cumulative counts, which represent the position of each element in the sorted array.
- Using the cumulative count, elements are placed in their correct sorted positions in the output array.
- Finally, the sorted elements from the output array are copied back into the original array and displayed.
To Wrap It Up:
Counting Sort in C++ is a non-comparison-based sorting algorithm that efficiently sorts integers by counting occurrences of each element. It works best when the range of elements is not significantly larger than the number of items in the array.
The algorithm uses extra space for the count and output arrays, making it stable and predictable with O(n + k) time complexity. Counting sort is also commonly used as a subroutine in algorithms like radix sort for enhanced performance.
FAQs
Counting Sort is a non-comparison-based sorting algorithm that counts the occurrence of each element and sorts them based on these counts. It is efficient for sorting integers within a known range.
The time complexity of Counting Sort is O(n + k), where n is the number of elements and k is the range of input values. It is faster than comparison-based algorithms for small ranges.
Yes, Counting Sort is stable, meaning it maintains the relative order of elements with equal values. This property makes it useful as a subroutine in algorithms like Radix Sort.
Counting Sort is best used when the input consists of integers in a small range. It is not suitable for large ranges or non-integer data as it can consume a lot of memory.
Sorting
Sorting algorithms are easy to learn but are really important for college semester exams and companies offering package between 3 – 6 LPA would ask direct searching questions in online test/ interviews.

Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment