sorter.c
static void countingSort(int* array, int length, int base)
{
   //base is the range of numbers
   //Hack, converting input array to all positive number, as our random generator now generates negative as well
   int i=0;
   for(i=0;i<length;i++)
   {
       if(array[i]<0)
          array[i]*= -1;
   }
   int* pos = malloc(base*sizeof(int));
   int* temp= malloc(length*sizeof(int));
   if(NULL==pos || NULL==temp)
   {
       printf("malloc failed \n");
       return;
   }
   memset(pos,0,base*sizeof(int));
   memset(temp,0,length*sizeof(int));
   for(i=0;i<length;i++)
   {
       pos[array[i]]++;
   }
   for(i=1;i<=base;i++)
   {
       pos[i] = pos[i] + pos[i-1];
   }
   /**
    * At this point the cumulative counts are obtained
    * Each index will be the sum of all the elements <= that number
    * For loop is in this order, so that elements later in the array will be added at the last possible position  ==> stability
    */
   for(i=length-1;i>=0;i--)
   {
       //We want to figure out where to put array[i], pos[array[i], gives including that number how many are there before it. -1 for 0 indexing
       temp[pos[array[i]] -1] = array[i];
       pos[array[i]]--;
   }
   memcpy(array,temp,length*sizeof(int));
   free(temp);
   free(pos);
}
/**
 * Basic idea is to find the number of elements less than the specific element and then directly put it in its correct position
 * Counting sort is of order O(N+K)
 * Space O(N+K)
 * initialise an counting array of length k (0-k-1) to zero
 * Go over the n inputs incrementing countingArray[n]
 * to finish the sort iterate over the countingArray, 
 */
Basic idea is to find the number of elements less than the specific element and then directly put it in its correct position
Counting sort is of order O(N+K)
Space O(N+K)
initialise an counting array of length k (0-k-1) to zero
Go over the n inputs incrementing countingArray[n]
to finish the sort iterate over the countingArray.
Basic idea is to find the number of elements less than the specific element and then directly put it in its correct position Counting sort is of order O ( N + K ) Space O ( N + K ) initialise an counting array of length k ( 0 - k - 1 ) to zero Go over the n inputs incrementing countingArray [ n ] to finish the sort iterate over the countingArray .
static void countingSort(int* array, int length, int base)
{
   int i=0;
   for(i=0;i<length;i++)
   {
       if(array[i]<0)
          array[i]*= -1;
   }
   int* pos = malloc(base*sizeof(int));
   int* temp= malloc(length*sizeof(int));
   if(NULL==pos || NULL==temp)
   {
       printf("malloc failed \n");
       return;
   }
   memset(pos,0,base*sizeof(int));
   memset(temp,0,length*sizeof(int));
   for(i=0;i<length;i++)
   {
       pos[array[i]]++;
   }
   for(i=1;i<=base;i++)
   {
       pos[i] = pos[i] + pos[i-1];
   }
   for(i=length-1;i>=0;i--)
   {
       temp[pos[array[i]] -1] = array[i];
       pos[array[i]]--;
   }
   memcpy(array,temp,length*sizeof(int));
   free(temp);
   free(pos);
}
static void countingSort ( int * array , int length , int base ) { int i = 0 ; for ( i = 0 ; i < length ; i ++ ) { if ( array [ i ] < 0 ) array [ i ] *= -1 ; } int * pos = malloc ( base * sizeof ( int ) ) ; int * temp = malloc ( length * sizeof ( int ) ) ; if ( NULL == pos || NULL == temp ) { printf ( " \n " ) ; return ; } memset ( pos , 0 , base * sizeof ( int ) ) ; memset ( temp , 0 , length * sizeof ( int ) ) ; for ( i = 0 ; i < length ; i ++ ) { pos [ array [ i ] ] ++ ; } for ( i = 1 ; i <= base ; i ++ ) { pos [ i ] = pos [ i ] + pos [ i - 1 ] ; } for ( i = length - 1 ; i >= 0 ; i -- ) { temp [ pos [ array [ i ] ] - 1 ] = array [ i ] ; pos [ array [ i ] ] -- ; } memcpy ( array , temp , length * sizeof ( int ) ) ; free ( temp ) ; free ( pos ) ; }
Basic idea is to find the number of elements less than the specific element and then directly put it in its correct position
 Counting sort is of order O(N+K)
 Space O(N+K)
 initialise an counting array of length k (0-k-1) to zero
 Go over the n inputs incrementing countingArray[n]
 to finish the sort iterate over the countingArray,
Basic idea is to find the number of elements less than the specific element and then directly put it in its correct position Counting sort is of order O ( N + K ) Space O ( N + K ) initialise an counting array of length k ( 0 - k - 1 ) to zero Go over the n inputs incrementing countingArray [ n ] to finish the sort iterate over the countingArray
//base is the range of numbers //Hack, converting input array to all positive number, as our random generator now generates negative as well /**
    * At this point the cumulative counts are obtained
    * Each index will be the sum of all the elements <= that number
    * For loop is in this order, so that elements later in the array will be added at the last possible position  ==> stability
    */ //We want to figure out where to put array[i], pos[array[i], gives including that number how many are there before it. -1 for 0 indexing