void countingsort(int *array1,int *array2,int maxkey,int num){
int keystart[maxkey+1]; //start position of each key
int start=0;
int i;
int temp;
for(i=0;i<=maxkey;i++){
keystart[i]=0;
}
for(i=0;i<num;i++){ //counting #key
keystart[array1[i]]++;
}
for(i=0;i<=maxkey;i++){ //counting the start position for each key
temp=keystart[i];
keystart[i]=start;
start=start+temp;
}
for(i=0;i<num;i++){ //move each element to the new position according to keystart[]
array2[keystart[array1[i]]++]=array1[i];
}
}
沒有留言:
張貼留言