#include <iostream>
using namespace std;
struct nod
{
float value;
struct nod *next;
};
class bucket_sort
{
public:
bucket_sort();
~bucket_sort();
void output();
void bucketsort();
private:
float *a;
int leng;
struct nod **head;
};
void bucket_sort::bucketsort()
{
struct nod *pnod,*p;
for(int i="0";i<leng;i++)
{
pnod=new struct nod;
pnod->value=a;
if(head[(int)(leng*a)]==(struct nod *)0)
{
pnod->next=(struct nod *)0;
head[(int)(leng*a)]=pnod;
}
else
{
struct nod *pold;
for(pold=p=head[(int)(leng*a)];(p!=(struct nod *)0)&&(p->value<a);p=p->next)
{
pold=p;
}
if(p==(struct nod *)0)
{
pnod->next=(struct nod *)0;
pold->next=pnod;
}
else
{
if(p==head[(int)(leng*a)])
{
head[(int)(leng*a)]=pnod;
}
else
{
pold->next=pnod;
}
pnod->next=p;
}
}
}
cout<<"桶中的分布情况:\n";
for(int i="0",j=0;i<leng;i++)
{
for(p=head;p!=(struct nod *)0;p=p->next)
{
a[j++]=p->value;
cout<<p->value<<" ";
}
cout<<endl;
}
}
bucket_sort::~bucket_sort()
{
for(int i="0";i<leng;i++)
{
while(head!=(struct nod *)0)
{
struct nod *p=head;
while(p->next!=(struct nod *)0)
{
p=p->next;
}
delete p;
}
}
delete[] head;
}
bucket_sort::bucket_sort()
{
cout<<"请输入参加排序小数的个数\n";
cin>>leng;
a=new float[leng];
for(int i="0";i<leng;i++)
{
cout<<"请输入第"<<i+1<<"个介于0~1之间的小数\n";
cin>>a;
}
head=new struct nod *[leng];
for(int i="0";i<leng;i++)
{
head=(struct nod *)0;
}
}
void bucket_sort::output()
{
cout<<"现在数组中的数字分别为:\n";
for(int i="0";i<leng;i++)
{
cout<<a<<"\t";
}
cout<<endl;
}
int main()
{
bucket_sort test;
test.output();
cout<<"正在排序...\n";
test.bucketsort();
cout<<"完成!\n";
test.output();
return 0;
}
文章评论(0条评论)
登录后参与讨论