diff --git a/sorting/radixsort.c b/sorting/radixsort.c index 8f274ede..6f0aa062 100644 --- a/sorting/radixsort.c +++ b/sorting/radixsort.c @@ -1,50 +1,83 @@ +//sorting of array list using Radix sort #include -#define range 10 -int MAX(int ar[], int size1){ - int i, max1 = ar[0]; - for(i = 0; imax1) - max1 = ar[i]; +#define range 10 // Range for integers is 10 as digits range from 0-9 + +// Utility function to get the maximum value in ar[] +int MAX(int ar[], int size){ + int i, max = ar[0]; + for(i = 0; imax) + max = ar[i]; } - return max1; + return max; } -void countsort(int arr[],int n,int place) +// Counting sort according to the digit represented by place +void countSort(int arr[],int n,int place) { - int i,freq[range]={0}; //range for integers is 10 as digits range from 0-9 + int i,freq[range]={0}; int output[n]; + + // Store count of occurences in freq[] for(i=0;i=0;i--) { output[freq[(arr[i]/place)%range]-1]=arr[i]; freq[(arr[i]/place)%range]--; } + + // Copy the output array to arr[], so it contains numbers according to the current digit for(i=0;i