gpt4 book ai didi

c++ - C++实现的基数排序

转载 作者:可可西里 更新时间:2023-11-01 17:36:27 26 4
gpt4 key购买 nike

我正在尝试通过创建一个程序来改进我的 C++,该程序将采用 1 到 10^6 之间的大量数字。将在每次传递中存储数字的存储桶是一个节点数组(其中节点是我创建的包含值和下一个节点属性的结构)。

根据最低有效值将数字排序到桶中后,我将一个桶的末尾指向另一个桶的开头(这样我就可以快速获取存储的数字而不会打乱顺序)。我的代码没有错误(无论是编译还是运行时),但我在如何解决剩余的 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) 即可对您的数组进行排序。

关于c++ - C++实现的基数排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1271367/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com