用 C++ 实现的基数排序
-
13-09-2019 - |
题
我正在尝试通过创建一个需要 1 到 10^6 之间大量数字的程序来提高我的 C++ 水平。将在每次传递中存储数字的存储桶是一个节点数组(其中节点是我创建的包含值和下一个节点属性的结构)。
根据最低有效值将数字排序到存储桶中后,我将一个存储桶的末尾指向另一个存储桶的开头(这样我就可以快速获取存储的数字而不破坏顺序)。我的代码没有错误(无论是编译还是运行时),但对于如何解决剩余的 6 次迭代,我遇到了困难(因为我知道数字的范围)。
我遇到的问题是最初数字以 int 数组的形式提供给 radixSort 函数。在第一次排序迭代之后,数字现在存储在结构数组中。有什么方法可以重新编写我的代码,以便我只有一个 for 循环进行 7 次迭代,或者我需要一个 for 循环运行一次,而其下面的另一个循环将运行 6 次,然后返回完全排序的结果列表?
#include <iostream>
#include <math.h>
using namespace std;
struct node
{
int value;
node *next;
};
//The 10 buckets to store the intermediary results of every sort
node *bucket[10];
//This serves as the array of pointers to the front of every linked list
node *ptr[10];
//This serves as the array of pointer to the end of every linked list
node *end[10];
node *linkedpointer;
node *item;
node *temp;
void append(int value, int n)
{
node *temp;
item=new node;
item->value=value;
item->next=NULL;
end[n]=item;
if(bucket[n]->next==NULL)
{
cout << "Bucket " << n << " is empty" <<endl;
bucket[n]->next=item;
ptr[n]=item;
}
else
{
cout << "Bucket " << n << " is not empty" <<endl;
temp=bucket[n];
while(temp->next!=NULL){
temp=temp->next;
}
temp->next=item;
}
}
bool isBucketEmpty(int n){
if(bucket[n]->next!=NULL)
return false;
else
return true;
}
//print the contents of all buckets in order
void printBucket(){
temp=bucket[0]->next;
int i=0;
while(i<10){
if(temp==NULL){
i++;
temp=bucket[i]->next;
}
else break;
}
linkedpointer=temp;
while(temp!=NULL){
cout << temp->value <<endl;
temp=temp->next;
}
}
void radixSort(int *list, int length){
int i,j,k,l;
int x;
for(i=0;i<10;i++){
bucket[i]=new node;
ptr[i]=new node;
ptr[i]->next=NULL;
end[i]=new node;
}
linkedpointer=new node;
//Perform radix sort
for(i=0;i<1;i++){
for(j=0;j<length;j++){
x=(int)(*(list+j)/pow(10,i))%10;
append(*(list+j),x);
printBucket(x);
}//End of insertion loop
k=0,l=1;
//Linking loop: Link end of one linked list to the front of another
for(j=0;j<9;j++){
if(isBucketEmpty(k))
k++;
if(isBucketEmpty(l) && l!=9)
l++;
if(!isBucketEmpty(k) && !isBucketEmpty(l)){
end[k]->next=ptr[l];
k++;
if(l!=9) l++;
}
}//End of linking for loop
cout << "Print results" <<endl;
printBucket();
for(j=0;j<10;j++)
bucket[i]->next=NULL;
cout << "End of iteration" <<endl;
}//End of radix sort loop
}
int main(){
int testcases,i,input;
cin >> testcases;
int list[testcases];
int *ptr=&list[0];
for(i=0;i<testcases;i++){
cin>>list[i];
}
radixSort(ptr,testcases);
return 0;
}
解决方案
我想你狠狠的过于复杂的解决方案。可以使用在输入端接收的单一阵列,其中在由该标记输入阵列中的每个桶的起始索引索引数组表示的每个步骤中的桶实现基数。
事实上,你甚至可以做到这一点递归:
// Sort 'size' number of integers starting at 'input' according to the 'digit'th digit
// For the parameter 'digit', 0 denotes the least significant digit and increases as significance does
void radixSort(int* input, int size, int digit)
{
if (size == 0)
return;
int[10] buckets; // assuming decimal numbers
// Sort the array in place while keeping track of bucket starting indices.
// If bucket[i] is meant to be empty (no numbers with i at the specified digit),
// then let bucket[i+1] = bucket[i]
for (int i = 0; i < 10; ++i)
{
radixSort(input + buckets[i], buckets[i+1] - buckets[i], digit+1);
}
}
当然buckets[i+1] - buckets[i]
会造成缓冲器溢出时i
为9,但我省略了额外的检查或可读性的缘故;我相信你知道如何处理这个问题。
通过这一点,你只需要调用radixSort(testcases, sizeof(testcases) / sizeof(testcases[0]), 0)
和您的阵列应该进行排序。
其他提示
要加快与更好的内存管理方法中,创建用于获得通过使阵列上方的单程转化成指数的计数的矩阵。分配第二临时阵列的大小与原始阵列相同,并且直到该数组的排序两个阵列之间的排序基数。如果通行证执行奇数基数排序的,那么临时阵列将需要被复制回末原始数组。
要进一步加快进程,使用碱256,而不是基体10为基数排序。这仅需要1个扫描通以创建矩阵和4基数排序传递到执行排序。示例代码:
typedef unsigned int uint32_t;
uint32_t * RadixSort(uint32_t * a, size_t count)
{
size_t mIndex[4][256] = {0}; // count / index matrix
uint32_t * b = new uint32_t [COUNT]; // allocate temp array
size_t i,j,m,n;
uint32_t u;
for(i = 0; i < count; i++){ // generate histograms
u = a[i];
for(j = 0; j < 4; j++){
mIndex[j][(size_t)(u & 0xff)]++;
u >>= 8;
}
}
for(j = 0; j < 4; j++){ // convert to indices
m = 0;
for(i = 0; i < 256; i++){
n = mIndex[j][i];
mIndex[j][i] = m;
m += n;
}
}
for(j = 0; j < 4; j++){ // radix sort
for(i = 0; i < count; i++){ // sort by current lsb
u = a[i];
m = (size_t)(u>>(j<<3))&0xff;
b[mIndex[j][m]++] = u;
}
std::swap(a, b); // swap ptrs
}
delete[] b;
return(a);
}
由于您的值是 0 范围内的整数...1,000,000
您可以创建一个大小为 1,000,001 的 int 数组,并分两遍完成整个过程
将第二个数组初始化为全零。
通过您的输入数组进行通过,并将值用作下标,以将第二个数组中的值递增。
一旦你做到了,那么第二遍就很容易了。穿过第二个数组,每个元素都会告诉您该数字出现在原始数组中的次数。使用该信息重新填充您的输入阵列。