给定n
32 位整数(假设它们是正数),您希望通过首先查看shift
总位中的最高有效位并递归地对由这些位上的排序整数创建的每个桶进行排序来对它们进行排序。
因此,如果shift
是 2,那么您将首先查看每个 32 位整数中的两个最高有效位,然后应用计数排序。最后,从您将获得的组中,您对每个组进行递归,并通过查看第三和第四个最高有效位开始对每个组的数字进行排序。您递归地执行此操作。
我的代码如下:
void radix_sortMSD(int start, int end,
int shift, int currentDigit, int input[])
{
if(end <= start+1 || currentDigit>=32) return;
/*
find total amount of buckets
which is basically 2^(shift)
*/
long long int numberOfBuckets = (1UL<<shift);
/*
initialize a temporary array
that will hold the sorted input array
after finding the values of each bucket.
*/
int tmp[end];
/*
Allocate memory for the buckets.
*/
int *buckets = new int[numberOfBuckets + 1];
/*
initialize the buckets,
we don't care about what's
happening in position numberOfBuckets+1
*/
for(int p=0;p<numberOfBuckets + 1;p++)
buckets[p] = 0;
//update the buckets
for (int p = start; p < end; p++)
buckets[((input[p] >> (32 - currentDigit - shift))
& (numberOfBuckets-1)) + 1]++;
//find the accumulative sum
for(int p = 1; p < numberOfBuckets + 1; p++)
buckets[p] += buckets[p-1];
//sort the input array input and store it in array tmp
for (int p = start; p < end; p++){
tmp[buckets[((input[p] >> (32 - currentDigit- shift))
& (numberOfBuckets-1))]++] = input[p];
}
//copy all the elements in array tmp to array input
for(int p = start; p < end; p++)
input[p] = tmp[p];
//recurse on all the groups that have been created
for(int p=0;p<numberOfBuckets;p++){
radix_sortMSD(start+buckets[p],
start+buckets[p+1], shift, currentDigit+shift, input);
}
//free the memory of the buckets
delete[] buckets;
}
int main()
{
int a[] = {1, 3, 2, 1, 4, 8, 4, 3};
int n = sizeof(a)/sizeof(int);
radix_sortMSD(0,n, 2,0,a);
return 0;
}
我可以想象这段代码中只有两个问题。
第一个问题是我是否真的在每次迭代中得到正确的整数位。我做了一个假设,如果我处于一个位置currentDigit
,如果currentDigit = 0
这意味着我在32
我的整数中,那么为了得到下一个shift
位,我按位右移32 - currentDigit - shift
,然后我应用 AND 运算来获得shift
最不重要的位,这正是我想要的位。
第二个问题是递归。我不认为我在正确的组上递归,但由于我不知道第一个问题是否真的得到了正确的解决,我目前不能对此多说。
对此的任何反馈将不胜感激。
先感谢您。
编辑:添加主函数以显示我的基数函数是如何被调用的。