Sort the elements of an array A in ascending order.

According to wikipedia, the counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items.  However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.

How it works?

Condition: elements should be in a range, in our example [25, 74]
Input data: 50 30 25 74 55 44

Step 1: initialize all elements of a count array to zero for all the range
index: 0 1 .....     50
count: 0 0 .....    0 
where index 0 corresponds to value 25, index 1 to value 26, ... index 50 to value 74

Step 2: store in count the number of occurence of each element of the input data
index: 0  ... 5 .... 19 .... 25 .... 30 .... 49 
count: 1 ... 1 ....  1 ....   1 ....    1 ....  1 

Step 3: modify the count array such that each element at each index stores the sum of previous counts
index: 0  ... 5 .... 19 .... 25 .... 30 .... 49 
count: 1 ... 2 ....  3 ....   4 ....    5 ....  6 
The modified count array indicates the position of each element in  the output

Step 4: output each element from the input sequence followed by decreasing its count by 1.


Difficulty level
This exercise is mostly suitable for students
#include<stdio.h>
#include<conio.h>
#define SIZE 50
#define RANGE1 25
#define RANGE2 74
void main()
{
	int A[SIZE], N;
	int i, j;
	int count[SIZE]; // array that counts the number 
			// of occurence of each element.
			// its size is equal to the size of A
			// if all elements are distincts.
			// However we don't know the range of  values
			// RESTRICTION: range of values between RANGE1 and RANGE2
			//		Make sure that RANGE2 - RANGE1 falls in SIZE
	int out[SIZE]; // sorted array

	do {
		printf("Enter the dimension: ");
		scanf("%d", &N);
	} while (N <= 0 || N > SIZE);

	for (i = 0; i < N; i++)
	{
		do{
			printf("Enter A[%d]: ", i);
			scanf("%d", &A[i]);
		}while(A[i]<RANGE1 || A[i]>RANGE2); // forcing the elements of the array to be within the range
	}

 	// counting sort 

	// step 2: put zeros in the count array
	for (i = 0; i <= RANGE2-RANGE1; i++) 
        	count[i] =0;  

	// step 2: populate the array count 
	// with the number of occurence of each element
	// ATTENTION: count[0] should contain the number of occurrence of the 
	//            first element in the range, i.e. 25 here 
	for(i = 0; i< N; i++) 
        	++count[A[i]-RANGE1]; 

	// step 3: Modify the count array such that each element at each index 
  	// stores the sum of previous counts. 
	for (i = 1; i <= RANGE2-RANGE1; i++) 
        	count[i] += count[i-1]; 

	// The modified count array indicates the position of each element in 
	// the output sequence and its number of occurence.
	
	// step 4: loop over A.
	//	put in the out array A[i] at index count[A[i]-RANGE1] - 1
	//	and decrease count. 
 	for (i = 0; i < N; i++) 
    	{ 
		out[count[A[i]-RANGE1] - 1]=A[i];  // -1 since array strats from index 0
		--count[A[i]-RANGE1];
    	} 

	printf("\n\n** After sorting ** \n");
	for (i = 0; i < N; i++)
	{
		printf("A[%d]=%d\n", i, out[i]);
	}

	getch();
}


Back to the list of exercises
Looking for a more challenging exercise, try this one !!
Sorting using the radix sort algorithm