排序

来源:互联网 发布:淘宝精品推荐女鞋 编辑:程序博客网 时间:2024/04/30 18:32
#include <iostream.h>
#include <malloc.h>
#define MAX 30
typedef int ElemType;


typedef struct 
{
int length;
ElemType r[MAX+1];
}SqList;
void Create(SqList &S)
{
int i;
cout<<"please input the mount that you want to order:"<<endl;
cin>>S.length;
cout<<"please input the data that you want to order:";
for(i=1;i<=S.length;i++)
{
cin>>S.r[i];
}
}
void InsertSort(SqList &S)
{
int i,j;
for(i=2;i<=S.length;i++)
{
if(S.r[i]<S.r[i-1])
{
S.r[0]=S.r[i];
S.r[i]=S.r[i-1];
for(j=i-2;S.r[0]<S.r[j];j--)
S.r[j+1]=S.r[j];
S.r[j+1]=S.r[0];
}
}
}
void BInsertSort(SqList &L)
{
int i,j,m,low,high;
for(i=2;i<=L.length;i++)
{
L.r[0]=L.r[i];
low=1;high=i-1;
while(low<=high)
{
m=(low+high)/2;
if(L.r[0]<L.r[m]) high=m-1;
else low=m+1;
}
for(j=i-1;j>=high+1;--j) L.r[j+1]=L.r[j];
L.r[high+1]=L.r[0];
}
}
void ShellInsert(SqList &L,int dk)
{
int i,j;
for(i=dk+1;i<=L.length;++i)
{
if(L.r[i]<L.r[i-dk])
{
L.r[0]=L.r[i];
for(j=i-dk;j>0 && L.r[0]<L.r[j];j-=dk)
L.r[j+dk]=L.r[j];
L.r[j+dk]=L.r[0];
}
}
}
void ShellSort(SqList &L,int dlta[],int t)
{
int k;
for(k=0;k<t;++k)
ShellInsert(L,dlta[k]);
}
void Print(SqList S)
{
int i;
for(i=1;i<=S.length;i++)
cout<<S.r[i]<<" ";
cout<<endl;
}


void main()
{
int i,k,a[MAX];
SqList S;
Create(S);
//InsertSort(S);
//BInsertSort(S);
cout<<"please input the methord you want to oreder:"<<endl;
cin>>k;
for(i=0;i<k;i++)
cin>>a[i];
ShellSort(S,a,k);
Print(S);
}
原创粉丝点击