What is Counting Sort?

Maam what is counting sort. In this course I have only read
Bubble Sort , Insertion Sort and Selection sort.

Counting sort is a sorting technique based on keys between a specific range. It works by counting the number of objects having distinct key values (kind of hashing). Then doing some arithmetic to calculate the position of each object in the output sequence.

Let us understand it with the help of an example.

For simplicity, consider the data in the range 0 to 9.

Input data: 1, 4, 1, 2, 7, 5, 2
  1) Take a count array to store the count of each unique object.
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  2  0   1  1  0  1  0  0

  2) Modify the count array such that each element at each index 
  stores the sum of previous counts. 
  Index:     0  1  2  3  4  5  6  7  8  9
  Count:     0  2  4  4  5  6  6  7  7  7

The modified count array indicates the position of each object in 
the output sequence.
 
  3) Output each object from the input sequence followed by 
  decreasing its count by 1.
  Process the input data: 1, 4, 1, 2, 7, 5, 2. Position of 1 is 2.
  Put data 1 at index 2 in output. Decrease count by 1 to place 
  next data 1 at an index 1 smaller than this index.

Here we assumed 1 indexed array
So now start with input array
1, 4, 1, 2, 7, 5, 2
Now count[1]=2
So place 1 at index 2(count[1]) in output array and do count[1]- - [- 1 - - - - - ]
Now place 4 at index 5(count[4]) and do count[4]- - [- 1 - - 4 - - ]
Now place 1 at index 1(count[1]) and do count[1]-- [1 1 - - 4 - - ]
Now place 2 at index 4(count[2]) and do count[2]-- [1 1 - 2 4 - - ]
Now place 7 at index 7(count[7]) and do count[7]-- [1 1 - 2 4 - 7 ]
Now place 5 at index 4(count[5]) and do count[5]-- [1 1 - 2 4 5 7 ]
Now place 2 at index 3(count[2]) and do count[2]-- [1 1 2 2 4 5 7 ]