struct Cell
{
  int data;
  int occur;
};

struct Cell* countingSort(struct Cell* A, int range)
{
  //Counting array
  int* C = (int*) malloc ((range+1) * sizeof(int));

  //Output array
  struct Cell* B = (struct Cell*) malloc (SIZE * sizeof(struct Cell));
  
  int i, temp;

  //Initialize Counting array, C
  for(i = 0; i <= range; i++)
    C[i] = 0;
  
  //C[i] contains how many occurence of element i in array A.
  /*
   * A = {1, 2, 5, 1, 2}
   * C = {2, 2, 0, 0, 1}, array A has two elements of value 1, two elements of value 2, and so on. 
   */
  for(i = 0; i < SIZE; i++)
    C[A[i].data] = C[A[i].data] + 1;
    
    
  //Now, C[i] contains the number of elements less or equal to i
  /*
   *C = {2, 4, 4, 4, 5}
   */
  for(i = 1; i <= range; i++)
    C[i] = C[i] + C[i-1];

  //Dispatch to output array 
  //  for(i = 0; i < SIZE; i++), it would be NOT stable
  for(i = SIZE - 1; i >= 0; i--)
    {
      B[C[A[i].data]-1].data = A[i].data;
      B[C[A[i].data]-1].occur = A[i].occur;
      C[A[i].data] = C[A[i].data] - 1;
    }

  return B;
}