Counting sort is a sorting algorithm that sorts the elements of an array by counting the number of occurrences of each unique element in the array. The count is stored in an auxiliary array and the sorting is done by mapping the count as an index of the auxiliary array.
max
) from the given array.Initialize an array of length max+1
with all elements 0. This array is used for storing the count of the elements in the array.
count
arraycount
array. If element “5” is not present in the array, then 0 is stored in 5th position.countingSort(array, size)
max <- find largest element in array
initialize count array with all zeros
for j <- 0 to size
find the total count of each unique element and
store the count at jth index in count array
for i <- 1 to max
find the cumulative sum and store it in count array itself
for j <- size down to 1
restore the elements to array
decrease count of each element restored by 1
# Counting sort in Python programming
def countingSort(array):
size = len(array)
output = [0] * size
count = [0] * 10
for i in range(0, size):
count[array[i]] += 1
for i in range(1, 10):
count[i] += count[i - 1]
i = size - 1
while i >= 0:
output[count[array[i]] - 1] = array[i]
count[array[i]] -= 1
i -= 1
for i in range(0, size):
array[i] = output[i]
data = [4, 2, 2, 8, 3, 3, 1]
countingSort(data)
print("Sorted Array in Ascending Order: ")
print(data)
// Counting sort in Java programming
import java.util.Arrays;
class CountingSort {
void countSort(int array[], int size) {
int[] output = new int[size + 1];
int max = array[0];
for (int i = 1; i < size; i++) {
if (array[i] > max)
max = array[i];
}
int[] count = new int[max + 1];
for (int i = 0; i < max; ++i) {
count[i] = 0;
}
for (int i = 0; i < size; i++) {
count[array[i]]++;
}
for (int i = 1; i <= max; i++) {
count[i] += count[i - 1];
}
for (int i = size - 1; i >= 0; i--) {
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}
for (int i = 0; i < size; i++) {
array[i] = output[i];
}
}
public static void main(String args[]) {
int[] data = { 4, 2, 2, 8, 3, 3, 1 };
int size = data.length;
CountingSort cs = new CountingSort();
cs.countSort(data, size);
System.out.println("Sorted Array in Ascending Order: ");
System.out.println(Arrays.toString(data));
}
}
// Counting sort in C programming
#include <stdio.h>
void countingSort(int array[], int size)
{
int output[10];
int max = array[0];
for (int i = 1; i < size; i++)
{
if (array[i] > max)
max = array[i];
}
// The size of count must be at least the (max+1) but
// we cannot assign declare it as int count(max+1) in C as
// it does not support dynamic memory allocation.
// So, its size is provided statically.
int count[10];
for (int i = 0; i <= max; ++i)
{
count[i] = 0;
}
for (int i = 0; i < size; i++)
{
count[array[i]]++;
}
for (int i = 1; i <= max; i++)
{
count[i] += count[i - 1];
}
for (int i = size - 1; i >= 0; i--)
{
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}
for (int i = 0; i < size; i++)
{
array[i] = output[i];
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; ++i)
{
printf("%d ", array[i]);
}
printf("\n");
}
int main()
{
int array[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(array) / sizeof(array[0]);
countingSort(array, n);
printArray(array, n);
}
// Counting sort in C++ programming
#include <iostream>
using namespace std;
void countSort(int array[], int size)
{
// The size of count must be at least the (max+1) but
// we cannot assign declare it as int count(max+1) in C++ as
// it does not support dynamic memory allocation.
// So, its size is provided statically.
int output[10];
int count[10];
int max = array[0];
for (int i = 1; i < size; i++)
{
if (array[i] > max)
max = array[i];
}
for (int i = 0; i <= max; ++i)
{
count[i] = 0;
}
for (int i = 0; i < size; i++)
{
count[array[i]]++;
}
for (int i = 1; i <= max; i++)
{
count[i] += count[i - 1];
}
for (int i = size - 1; i >= 0; i--)
{
output[count[array[i]] - 1] = array[i];
count[array[i]]--;
}
for (int i = 0; i < size; i++)
{
array[i] = output[i];
}
}
void printArray(int array[], int size)
{
for (int i = 0; i < size; i++)
cout << array[i] << " ";
cout << endl;
}
int main()
{
int array[] = {4, 2, 2, 8, 3, 3, 1};
int n = sizeof(array) / sizeof(array[0]);
countSort(array, n);
printArray(array, n);
}
Time Complexities:
There are mainly four main loops. (Finding the greatest value can be done outside the function.)
for-loop | time of counting |
---|---|
1st | O(max) |
2nd | O(size) |
3rd | O(max) |
4th | O(size) |
Overall complexity = O(max)+O(size)+O(max)+O(size)
= O(max+size)
O(n+k)
O(n+k)
O(n+k)
In all the above cases, the complexity is same because no matter how the elements are placed in the array, the algorithm goes through n+k
times.
There is no comparison between any elements so, it is better than comparison based sorting techniques. But, it is bad if the integers are very large because the array of that size should be made.
Space Complexity:
The space complexity of Counting Sort is O(max)
. Larger the range of elements, larger is the space complexity.
Counting sort is used when: