我复制了完整的c++办基数排序的算法实现如下:
for(int i = 1;i<10;i++) {
bucket[i] +=bucket[i-1];//这句话到底是做什么的? wiki上面说是 //将tmp中的位置依次分配给每个桶 我表示还是看不懂?
}
class RadixSort {
public:
int* radixSort(int* A, int n) {
// write code here
int bucket[10] ={0};
int count = getMaxCount(A,n);
int index;
int *temp = new int[n];
for(int k = 1;k<=count;k++) {
for(int i = 0;i<10;i++) {
bucket[i] = 0;
}
for(int i = 0;i<n;i++) {
index = getValueAt(A[i],k);
bucket[index]++;
}
for(int i = 1;i<10;i++) {
bucket[i] +=bucket[i-1];//这句话到底是做什么的? wiki上面说是 //将tmp中的位置依次分配给每个桶 我表示还是看不懂?
}
for(int i = n-1;i>=0;i--) {
int pos = -- bucket[getValueAt(A[i],k)];
temp[pos] = A[i];
}
for(int i = 0;i < n; i++) {
A[i] = temp[i];
}
}
delete[] temp;
return A;
}
int getMaxCount(int* A,int n) {
int max = A[0];
int count = 1;
for(int i= 1;i<n;i++) {
if(A[i]>max) {
max = A[i];
}
}
while(max/10 > 0) {
count++;
max /= 10;
}
return count;
}
int getValueAt(int val,int pos) {
while(--pos) {
val /= 10;
}
return val%10;
}
I’ll use the code on the wiki to explain:
I rewrote the code here and marked the sections with comments to facilitate discussion.
Let me give you an example to illustrate. Suppose we want to sort
data
as follows:We sort based on decimal, so we need to prepare 10 buckets numbered 0~9.
If you have a basic understanding of radix sort, you will know that we will perform d rounds in sequence, and each round uses the k-th bit to allocate buckets. In this example, the largest number is a two-digit number, so:
We’re only talking about what happens in one turn here. Suppose we now proceed to the first round, which is to use single digits to assign sorting.
Section 1
Nothing special here, we will clear all values from 0~9 of
count
to 0.Section 2
k = (data[j] / radix) % 10;
is to calculate the k-th digit of the integerdata[j]
, which in this round represents the single digit ofdata[j]
.So what this paragraph means is to put each data
data[j]
into the corresponding bucket according to its single digit number. Although we did not actually generate buckets to store the data, the correspondingcount
reflects the bucket. There are several data items inside.Section 3
Here is the key. The questioner’s problem lies here, but to understand this, you must understand the next paragraph. Let’s first observe what he does. Here he adds up the
count
corresponding to each bucket. stand up.0
0
)0
0
)3
3
)3
3
)4
4
)6
6
)7
7
)8
8
)9
9
)10
Section 4
This paragraph is the second key. Here we place it in order from the last item of
data
totmp
, and complete the task of sorting according to the k-th position in this round.Then what does the allocation below mean:
In fact, we can look at the meaning of the action in segment 3 like this:
0
0
)0
0
)3
3
)3
3
)4
4
)6
6
)7
7
)8
8
)9
9
)10
You will find:
original count
represents the amount of data in the bucket, and also represents how manytmp
indexfinal count
represents the maximum index allocated to the data in the bucket plus 1Taking bucket number 5 as an example, the two numbers including 25 and 35 occupy the first 6 (
tmp
) positions offinal count
, so count back two idx from6
,4
and5
are the positions of 25 and 35 intmp
respectively.That’s why we start from the last numbers in this paragraph (first 35 and then 25), 35 is placed in
count[k]-1
(5), thencount[k]--
, so that 25 is placed incount[k]-1
(4) .Section 5
Nothing special, copy the data in
tmp
back todata
to complete the sorting of this roundConclusion
I hope the above explanation can help you understand a little bit
Questions I answered: Python-QA