【发布时间】:2013-03-03 18:10:19
【问题描述】:
我为我的 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 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.
}
问题是,我真的不明白“组”是什么意思。我首先尝试使用数组,但当然,它会覆盖数字。如何根据最后一位数字对数字进行分组?我不要求任何代码。我只需要了解。非常感谢你。
【问题讨论】:
-
它说“可以使用队列作为存储桶来实现 LSD 基数排序”。但在 C 代码示例中,作者只是将存储桶用作数组。