2013-03-03 64 views
0

我有我的cs類的排序算法分配。我需要將Radix Sort的僞代碼轉換爲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. 
    } 

問題是,我真的不明白「組」是什麼意思。我首先嚐試使用數組,但當然,它會覆蓋數字。我如何根據最後一位數字對號碼進行分組?我沒有要求任何代碼。我只需要了解。非常感謝你。

+0

上http://en.wikipedia.org/wiki/Radix_sort很好的解釋 – nKandel 2013-03-03 18:13:24

+0

它說,「LSD基數排序可以使用隊列作爲水桶來實現。」但在C代碼示例中,作者只是使用一個存儲桶作爲數組。 – jdyg 2013-03-03 18:53:14

回答

0

由10提高到x次方每次取數,鴻溝,其中x是循環的每次迭代,然後通過10

(num/10**x) % 10 

這將在給你最顯著位模組,數字第一遍,然後按照從右到左的順序移動到每個數字。迭代次數應該等於最大數的長度。