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.
    }

問題は、「グループ」の意味がよくわからないことです。最初に配列を処理しようとしますが、もちろん、数値をオーバーライドします。最後の桁に従って数字をグループ化するにはどうすればよいですか? 私はコードを求めていません。私はただ理解する必要があります。どうもありがとうございました。

4

1 に答える 1

0

各数値を x 乗した 10 で割り、x をループの各反復として、その数値を 10 で mod します。

(num/10**x) % 10

これにより、最初のパスで最下位桁が得られ、次に右から左の順に各桁に移動します。反復回数は、最大数の長さに等しくなければなりません。

于 2014-04-11T01:31:08.837 に答える