Sorting 基数排序:什么是;“团体”;是用基数排序的吗?
我有我的cs类排序算法作业。我需要把基数排序的伪代码转换成C++。这是我的伪代码:Sorting 基数排序:什么是;“团体”;是用基数排序的吗?,sorting,radix,Sorting,Radix,我有我的cs类排序算法作业。我需要把基数排序的伪代码转换成C++。这是我的伪代码: radixSort( int theArray[], in n:integer, in d:integer) // sort n d-digit integers in the array theArray for (j=d down to 1) { Initialize 10 groups to empty Initialize a counter for each
radixSort( int theArray[], in n:integer, in d:integer)
// sort n d-digit integers in the array theArray
for (j=d down to 1) {
Initialize 10 groups to empty
Initialize a counter for each group to 0
for (i=0 through n-1) {
k = jth digit of theArray[i]
Place theArray[i] at the end of group k
Increase kth counter by 1
}
Replace the items in theArray with all the items in
group 0, followed by all the items in group 1, and so on.
}
问题是,我真的不明白“团体”是什么意思。我尝试先处理数组,但当然,它会覆盖数字。如何根据数字的最后一位对数字进行分组?我没有要求任何代码。我只是需要理解。多谢各位 取每个数字,除以10,再乘以x的幂,x是循环的每次迭代,然后将该数字mod 10
(num/10**x) % 10
这将在第一次传递时给出最低有效数字,然后按从右到左的顺序移动到每个数字。迭代的次数应该等于最大数字的长度。关于它的很好的解释是“可以使用队列作为桶来实现LSD基数排序。”但在C代码示例中,作者只使用桶作为数组。