This function use counting sort.
This code will only work for numbers>1. To include 0 or negative number, the function can be slightly modified.
To work with it send parameter as (array_name,size_of_array,number_range). where number range is the highest possible value in the given data array.
void counting_sort(int arr[], int sz,int range) { int i,j; int tmp[100]; memset(tmp,0,sizeof(tmp)); for(i=0;i<sz;i++) tmp[arr[i]]++; for(i=0,j=0;i<range;i++) { while(tmp[i]>0) { arr[j++]=i; tmp[i]--; } } }
This code will only work for numbers>1. To include 0 or negative number, the function can be slightly modified.
No comments:
Post a Comment