【问题标题】:Radix Sort: what does "groups" means in radix sort?基数排序:基数排序中的“组”是什么意思?
【发布时间】: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 代码示例中,作者只是将存储桶用作数组。

标签: sorting radix


【解决方案1】:

取每个数字,除以 10 的 x 次方,x 是循环的每次迭代,然后将该数字除以 10。

(num/10**x) % 10

这将在第一次传递时为您提供最低有效数字,然后按从右到左的顺序移动到每个数字。迭代次数应该等于最大数的长度。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-26
    • 1970-01-01
    • 1970-01-01
    • 2019-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多