Back to Course


0% Complete
0/82 Steps
  1. Getting Started with Algorithm
    What is an Algorithm?
  2. Characteristics of Algorithm
    1 Topic
  3. Analysis Framework
  4. Performance Analysis
    3 Topics
  5. Mathematical Analysis
    2 Topics
  6. Sorting Algorithm
    Sorting Algorithm
    10 Topics
  7. Searching Algorithm
    6 Topics
  8. Fundamental of Data Structures
  9. Queues
  10. Graphs
  11. Trees
  12. Sets
  13. Dictionaries
  14. Divide and Conquer
    General Method
  15. Binary Search
  16. Recurrence Equation for Divide and Conquer
  17. Finding the Maximum and Minimum
  18. Merge Sort
  19. Quick Sort
  20. Stassen’s Matrix Multiplication
  21. Advantages and Disadvantages of Divide and Conquer
  22. Decrease and Conquer
    Insertion Sort
  23. Topological Sort
  24. Greedy Method
    General Method
  25. Coin Change Problem
  26. Knapsack Problem
  27. Job Sequencing with Deadlines
  28. Minimum Cost Spanning Trees
    2 Topics
  29. Single Source Shortest Paths
    1 Topic
  30. Optimal Tree Problem
    1 Topic
  31. Transform and Conquer Approach
    1 Topic
  32. Dynamic Programming
    General Method with Examples
  33. Multistage Graphs
  34. Transitive Closure
    1 Topic
  35. All Pairs Shortest Paths
    6 Topics
  36. Backtracking
    General Method
  37. N-Queens Problem
  38. Sum of Subsets problem
  39. Graph Coloring
  40. Hamiltonian Cycles
  41. Branch and Bound
    2 Topics
  42. 0/1 Knapsack problem
    2 Topics
  43. NP-Complete and NP-Hard Problems
    1 Topic
Lesson 6, Topic 8
In Progress

Counting Sort

Lesson Progress
0% Complete

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.

How Counting Sort Works?

1. Find out the maximum element (let it be max) from the given array.

KodNest 1 4

2. 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.

KodNest 2 2

3. Store the count of each element at their respective index in count array

For example: If the count of element “4” occurs 2 times then 2 is stored in the 4th position in the count array. If element “5” is not present in the array, then 0 is stored in 5th position.

Store cumulative sum of the elements of the count array.

It helps in placing the elements into the correct index.

If there are x elements less than y, its position should be at x-1.

KodNest 3 2

4.For example: In the array below, the count of 4 is 6. It denotes that there are 5 elements smaller than 4. Thus, the position of 4 in the sorted array is 5th.

KodNest 5 1

5. Find the index of each element of the original array in count array. This gives the cumulative count. Place the element at the index calculated.

6. After placing each element at its correct position, decrease the its count by one.

Counting Sort Algorithm

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 C

// 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++)
  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];
  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]);
int main()
  int array[] = {4, 2, 2, 8, 3, 3, 1};
  int n = sizeof(array) / sizeof(array[0]);
  countingSort(array, n);
  printArray(array, n);


Time Complexities:
There are mainly four main loops. (Finding the greatest value can be done outside the function.)

for-looptime of counting

Overall complexity = O(max)+O(size)+O(max)+O(size) = O(max+size)

  • Worst Case Complexity: O(n+k)
  • Best Case Complexity: O(n+k)
  • Average Case Complexity: 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 Applications

Counting sort is used when:

  • there are smaller integers of multiple counts.
  • linear complexity is the need.
KodNest Training New Batch is starting on 19th October 2020. Attend one week free demo classes.Register Now

New Report