Introduction
Count Sort is a Linear Sorting algorithm that sorts elements in O(n) time, the other linear sorts include Bucket and Radix sorts.
What is Linear Sorting Algorithm
A Sorting algorithm that does not use any comparison operator ( >,<, >=, <=, = = ) to determine the sorting order of the elements, the sorting is achieved by acute logic build, and the overall time taken by the algorithm is hence linear.
Actually, if we contrast linear sorts to other comparison sorts with respect to time we will find that comparison sorts can do n log n at their best and exponential at worse in terms of time, the linear sort gives linear performance and thus have a fine edge in time over these algorithms.
Note: Along with Time, we also have to look at the Space/Memory usage of a particular algorithm, and actually this is exactly where Linear Sort falls back, as Linear Sorts use extra space roughly more than that of the original sorted data structure.
The Internal Working of Count Sort
The Count Sort is quite simple as compared to other sorting algorithms ( not even a single nested loop ), I will explain the logical working of count sort, and for plainness purposes use an array data structure.
There are only two Classes in the code namely :
- CountSort: This class contains methods Max, Count_Sort ( ), and Display ( ), for finding the Maximum element in an array, sorting the array with Count Sort, and for display all the elements in the array, respectively.
- CountSortApp: This class contains the Main() method that contains the Count Sort class object, calls to methods of Count Sort, and does nothing more.
The Count Sort Class have only two attributes
class CountSort
{
int []thearray; // the array of unsorted elements
int i;
.....
Now go through the Constructor of CountSort
public CountSort(int size)
{
thearray = new int [size] ;
Random ran = new Random();
for(int i = 0 ; i< size; i++)
{
thearray[i] = ran.Next(i,i*size);
}
}
1) The Constructor takes the size of the array and made thearray of this size.
2) Initialize thearray to Random numbers, first the Class Random object is declared i-e ran, and later uses its method Next(lowerbound, upperbound) to generate a random number at each iteration and equate it to thearray [ i ], so in all thearray is filled up with random numbers.
Next, is the Max() method, actually, the soul purpose of this method is to find the maximum number in the thearray.
// Find maximum Number in the Array
// This will take O(n) time
private int Max()
{
int max = thearray[0]; // first element be the max
for(i = 1 ; i<thearray.Length ;i++)
{
if (thearray[i] > max )
{
// if the elemnt at index 'i ' is greater than
//previous max , than set this value to the max
max = thearray[i] ;
}
//end of if
}
//end of for
return max;
}
Let's go through this method's code step by step.
3) First lets take the first element in the array to be the maximum number in the whole array i-e max = thearray[0]
Now iterate over the whole array except the first element (as we already select it as a maximum element), and whenever you find the element greater than the value contained in variable max, just replace the value of max with this value, actually, the check if (thearray[i] > max ) do the checking of element at index i with the current value of max, and in the case, if it is greater, replace it with max = thearray[i]
Now we come to the Count_Sort method, which contains all the real work.
First, there is the declaration for variables used by the CountSort method
int k = Max();
// the maximum element in the array
int []output = new int[thearray.Length];
int []temp = new int[k+1]; //For indeing up to k , we required array of k+1
4) Declaration of variable k which holds the maximum element in thearray, is done so by calling Max() method.
5) Declaration of array output of type int, equals in size of the original array. The output array will have final sorted values, as you can see that in order to have the original array sorted you have to copy from the output array to the original array ie thearray. I have more to say about this and other crucial facts in the last section.
6) Finally the declaration of array temp, which will provide the indexing up to the max value in thearray, temp array is of size k+1, since in C#, in order to have indexing up to 'k' we need to declare it of size k+1.
Now, we have a bunch of for loops, I will go through each of them separately, in order to clarify their functionality
The first for loop
for( i= 0 ; i<k+1 ; i++)
{
temp[ i ] = 0;
}
7) The initialization of the array temp to 0, actually this step is done in order to keep clarity and pace with the algorithm. What is done is only to make sure that every element of temp is set to zero, as this zero will play important role in the next coming role.
The Second for loop
for(i = 0 ; i<thearray.Length ; i++)
{
temp [ thearray [ i ] ] = temp [ thearray [ i ] ] + 1;
}






Ritambhara TechPosted Sep 5, 2012, 9:37 AM
Below link has a comparison of asymptotic time taken by different sorting algorithms http://www.ritambhara.in/comparison-of-sorting-algoritms/