Skip to content

Instantly share code, notes, and snippets.

@raunaqbn
Created July 2, 2016 02:43
Show Gist options
  • Save raunaqbn/fe135f0158b8dec5112bb6df45692545 to your computer and use it in GitHub Desktop.
Save raunaqbn/fe135f0158b8dec5112bb6df45692545 to your computer and use it in GitHub Desktop.
void countSort(int* arr,int size, int exp)
{
int output[size];
int count[10];
for(int i=0; i<size; i++)
count[(arr[i]/exp)%10]++;
for(int i=1; i<10; i++)
count[i] += count[i-1];
for(int i=0; i<size; i++)
{
output[count[(arr[i]/exp)%10]-1]= arr[i];
count[(arr[i]/exp)%10]--;
}
memcpy(arr,output,size);
}
void radixSort(int* arr,int size)
{
// find largest value in array to get max no of digits
int max = max(arr,n);
for (int exp=1; max/exp > 0; exp*=10)
countSort(arr,size,exp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment