六种排序
【测试数据】
由随机数产生器生成。
#define max 200
#include<iostream>
using namespace std;
typedef struct redtype
{
int key;
char otherinfo;
}redtype;
typedef struct sqlist
{
redtype r[max+1];
int length;
}sqlist;
void initlist(sqlist &l)
{
cout<<"请输入数据长度:"<<endl;
int n;
cin>>n;
l.length=n;
cout<<"随机数产生"<<n<<"个数据关键字"<<endl;
for(int i=1;i<=n;i++)
{
l.r[i].key=rand()/100;
cout<<l.r[i].key<<" ";
if(i%10==0)cout<<endl;
//cout<<"请输入第"<<"i"<<"个数据其他数据项"<<endl;
//cin>>l.r[i].otherinfo;
}
}
void insertsort(sqlist &l)
{
int i,j;
for(i=2;i<=l.length;i++)
if(l.r[i].key<l.r[i-1].key)
{
l.r[0].key=l.r[i].key;
l.r[i].key=l.r[i-1].key;
for(j=i-2;l.r[0].key<l.r[j].key;j--)
l.r[j+1].key=l.r[j].key;
l.r[j+1].key=l.r[0].key;
}
cout<<"直接插入排序后:"<<endl;
for(i=1;i<=l.length;i++)
{
cout<<l.r[i].key<<" ";
if(i%10==0)cout<<endl;
}
}
//希尔排序
void shellsort()
{
int i,j,g,n;
int a[200];
cout<<"请输入产生随机数个数:"<<endl;
cin>>n;
cout<<"产生随机数为:"<<endl;
for(i=1;i<=n;i++)
{
a[i]=rand()/100;
cout<<a[i]<<" ";
if(i%10==0)cout<<endl;
}
for(g=n/2;g>0;g=g/2)
{
for(j=g;j<=n;j++)
{
if(a[j]<a[j-g])
{
int temp=a[j];
int k=j-g;
while(k>=0&&a[k]>temp)
{
a[k+g]=a[k];
k=k-g;
}
a[k+g]=temp;
}
}
}
cout<<endl<<"希尔排序后:"<<endl;
for(i=1;i<=n;i++)
{
cout<<a[i]<<" ";
if(i%10==0)cout<<endl;
}
}
//快速排序
int partition(sqlist &l,int low,int h)
{
l.r[0].key=l.r[low].key;
int p;
p=l.r[low].key;
while(low<h)
{
while(low<h&&l.r[h].key>=p)h--;
l.r[low].key=l.r[h].key;
while(low<h&&l.r[low].key<=p)low++;
l.r[h].key=l.r[low].key;
}
l.r[low].key=l.r[0].key;
return low;
}
void qsort(sqlist &l,int low,int h)
{
int p;
if(low<h)
{
p=partition(l,low,h);
qsort(l,low,p-1);
qsort(l,p+1,h);
}
}
void quicksort(sqlist &l)
{
qsort(l,1,l.length);
cout<<endl<<"快速排序后为:"<<endl;
for(int i=1;i<=l.length;i++)
{
cout<<l.r[i].key<<" ";
if(i%10==0)cout<<endl;
}
}
//简单选择排序
int selectmin(sqlist &l,int n)//选择最小
{
int min;
min=n;
for(int i=n;i<=l.length;i++)
{
if(l.r[min].key>l.r[i].key)
min=i;
}
return min;
}
void selectsort(sqlist &l)
{
int i,j,t;
for(i=1;i<=l.length;i++)
{
j=selectmin(l,i);
if(i!=j)
{
t=l.r[i].key;
l.r[i].key=l.r[j].key;
l.r[j].key=t;
}
}
cout<<"简单选择排序后为:"<<endl;
for(i=1;i<=l.length;i++)
{
cout<<l.r[i].key<<" ";
if(i%10==0)cout<<endl;
}
}
//Heap Sort
void heapadjust(sqlist &a,int s,int end)
{
int t=a.r[s].key;
int i=2*s;//its left child
while(i<=end)
{
if(i+1<=end&&a.r[i+1].key>a.r[i].key)
i++;
if(a.r[i].key<=t)
break;
a.r[s].key=a.r[i].key;
s=i;
i=2*i;
}
a.r[s].key=t;
}
void heapsort(sqlist &a)
{
int i;
for(i=a.length/2;i>0;i--)
heapadjust(a,i,a.length);
for(i=a.length;i>1;i--)
{
int t=a.r[i].key;
a.r[i].key=a.r[1].key;
a.r[1].key=t;
heapadjust(a,1,i-1);
}
cout<<"堆排序后为:"<<endl;
for(i=1;i<=a.length;i++)
{
cout<<a.r[i].key<<" ";
if(i%10==0)cout<<endl;
}
}
//bubble sort
void bubble(sqlist &s)
{
int i,t,j;
for(i=1;i<s.length;i++)
{
for(j=1;j<=s.length-i;j++)
{
if(s.r[j].key>s.r[j+1].key)
{
t=s.r[j].key;
s.r[j].key=s.r[j+1].key;
s.r[j+1].key=t;
}
}
}
cout<<"冒泡排序后为:"<<endl;
for(i=1;i<=s.length;i++)
{
cout<<s.r[i].key<<" ";
if(i%10==0)cout<<endl;
}
}
int main()
{
sqlist sq;
initlist(sq);
insertsort(sq);
initlist(sq);
quicksort(sq);
shellsort();
initlist(sq);
selectsort(sq);
initlist(sq);
heapsort(sq);
initlist(sq);
bubble(sq);
return 0;
}