C++初级问题,桶排序,请达人帮忙修改一下

2024-12-17 17:23:37
推荐回答(3个)
回答1:

程序第四行函数声明 与第25行函数定义中 多维数组除第一维外其余必须指定

void bucketSort(int [],int,int[][],int,int);
void bucketSort(int a[],int size1,int b[][],int size2,int size3)

如果未知,可以采用指针传递
4 void bucketSort(int [],int,int *,int,int);
25 void bucketSort(int a[],int size1,int b*,int size2,int size3)

原来的 b[i][j]
改为 b[i*size3+j]

声明和定义的函数名不一致
5 int getlargest(int[],int);
56 int getLargest(int a[],int size1)

在收集过程,counter 清零的位置不对

执行下一次 c 前辅助数组 b 重新复位

#include
#include
using namespace std;
void bucketSort(int *,int,int*,int,int);
int getlargest(int * ,int);
int main()
{
const int SIZE=10;
int array[]={23,48,484,847,333,499,2};
int number=sizeof(array)/4;
int bucket[SIZE][number];
cout<<"Number:"< cout<<"orignal array"< for(int i=0;i cout< for(int row=0;row<10;row++)
{
for(int col=0;col bucket[row][col]=-1;
}
bucketSort(array,number,(int *)bucket,10,number);
//output the array sorted
cout<<"sorted array "< for(int output=0;output cout< return 0;
}
//a[] is the array to be sorted;b[][] can be treated as a tool,or as a bucket;every row is a bucket;
//size2 is from 0-9;size3 is the number that is to be sorted.
//void bucketSort(int a[],int size1,int b[][],int size2,int size3)
//传递多维数组需要知道除第一维外的其他维的大小,指针传递则方便的多
//b[i][j] 变为 b[i*size+j] 其中size 是每行的列数
void bucketSort(int * a,int size1,int * b,int size2,int size3)
{
int largest=getlargest(a,size1);
int places=0;
do {
places++;
}while(largest>=pow(10.,places));//Note a short of 1!!!!
//every place has to be sorted once.

for(int c=0;c//distributing process
for(int i=0;i for(int j=0;j }
for(int i=0;i int temp=int((a[i]/pow(10.,c)))%10;
b[temp*size3+i]=a[i];
}
//collecting process
int counter=0;//在此清
for(int j=0;j for(int k=0;k if(b[j*size3+k]!= -1){//note!!
a[counter]=b[j*size3+k];
counter++;
}
}
}
}
}

int getlargest(int * a,int size1)
{
int largest=a[0];
for(int i=0;i {
if(a[i]>largest)
largest=a[i];
}
return largest;
}
运行通过了。

回答2:

看看我该得,主要是改的2维数组定义及初始化
#include
#include
using namespace std;
void bucketSort(int [],int,int**,int,int);
int getlargest(int[],int);
int main()
{
const int SIZE=10;
int array[]={23,48,484,847,333,499,2};
int number=sizeof(array)/4;
int **bucket;//定义数组
bucket=new int *[SIZE];
for(int i=0;i {
bucket[i]=new int [number];
}
for(int row=0;row {
for(int col=0;col bucket[row][col]=-1;
}
bucketSort(array,number,bucket,10,number);
//output the array sorted
for(int output=0;output cout< cout< for (int i=0;i {
delete []bucket[i];
}
delete []bucket;
return 0;
}
//a[] is the array to be sorted;b[][] can be treated as a tool,or as a bucket;every row is a bucket;
//size2 is from 0-9;size3 is the number that is to be sorted.
void bucketSort(int a[],int size1,int **b,int size2,int size3)
{
int largest=getlargest(a,size1);
int places=0;
do
{
places++;
}while(largest>=pow((double)10,(double)places));//Note a short of 1!!!!
//every place has to be sorted once.
for(int c=0;c {
//distributing process
for(int i=0;i {
int temp=int((a[i]/pow((double)10,(double)c)))%10;
b[temp][i]=a[i];
}
//collecting process
for(int j=0;j {
for(int k=0;k {
int counter=0;
if(b[j][k]!= -1)//note!!
a[counter++]=b[j][k];
}
}
}

}

int getlargest(int a[],int size1)
{
int largest=a[0];
for(int i=0;i {
if(a[i]>largest)
largest=a[i];
}
return largest;
}

回答3:

修改了函数声明以及算法中间的实现的一些地方,结果运行正确

#include
#include
using namespace std;

int getLargest(int*a,int size1);
void bucketSort(int *a,int size1,int*b,int size2,int size3);

int main()
{
const int SIZE=10;
int array[]={23,48,484,847,333,499,2};
int number=sizeof(array)/4;
//int bucket[SIZE][number];
int *bucket = new int[SIZE*number];
for(int row=0;row<10;row++)
{
for(int col=0;col *(bucket+row*number+col)=-1;
}
bucketSort(array,number,bucket,10,number);
//output the array sorted
for(int output=0;output cout< delete []bucket;
return 0;
}
//a[] is the array to be sorted;b[][] can be treated as a tool,or as a bucket;every row is a bucket;
//size2 is from 0-9;size3 is the number that is to be sorted.
void bucketSort(int *a,int size1,int*b,int size2,int size3)
{
int largest=getLargest(a,size1);
int places=0;
do
{
places++;
}while(largest>=pow((float)10,places));//Note a short of 1!!!!
//every place has to be sorted once.
for(int c=0;c {
//distributing process
for(int i=0;i {
int temp=int((a[i]/pow((float)10,c)))%10;
*(b+temp*size3+i)=a[i];
}
//collecting process
int counter=0;
for(int j=0;j {
for(int k=0;k {

if(*(b+j*size3+k)!= -1)//note!!
{
a[counter++]=*(b+j*size3+k);
*(b+j*size3+k) = -1;
}
}
}
}
return;
}

int getLargest(int*a,int size1)
{
int largest=a[0];
for(int i=0;i {
if(a[i]>largest)
largest=a[i];
}
return largest;
}