//基数排序,最大位数,每趟排序采取计数排序
void radix_sort(int *data, int ncount, int maxdigits){
int base = 10;
int countArray[10];
int i, j;
int *temp = new int[ncount];
int t = 1; //相当于移位
for( i = 0; i < maxdigits; i++ ) {
//初始化计数数组
memset(countArray, 0, sizeof(countArray)/sizeof(int));
for( j = 0; j < ncount; j++ )
countArray[data[j]/t%base]++;
//计算计算数组中每个字符的起始位置
for( j = 1; j < 10; j++ )
countArray[j] += countArray[j-1];
for( j = ncount-1; j >= 0; j-- ){
int splitdigit = data[j]/t%base;
temp[countArray[splitdigit]-1] = data[j];
countArray[splitdigit]--;
}
memcpy_s(data, ncount, temp, ncount);
t = t * base;
}
}
基于计数排序的基数排序
最新推荐文章于 2023-11-25 14:02:51 发布