我的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.
}问题是,我真的不明白“组”是什么意思。我尝试先处理数组,但是,当然,它覆盖了数字。如何根据数字的最后一位数对数字进行分组?我没有要求任何代码。我只想弄明白。非常感谢。
发布于 2014-04-11 09:31:09
取每个数字,除以10的x的幂,x是循环的每次迭代,然后将该数字除以10。
(num/10**x) % 10这将在第一次传递时给出最低有效位,然后按从右到左的顺序移动到每个数字。迭代次数应等于最大值的长度。
https://stackoverflow.com/questions/15188955
复制相似问题