共计 845 个字符,预计需要花费 3 分钟才能阅读完成。
#include <iostream>
using namespace std;
#define MAXSIZE 20
typedef struct{
int key;
char *otherinfo;
}ElemType;
typedef struct{
ElemType *r;
int length;
}SqList;
// 一趟增量是 dk 的希尔插入排序
void ShellInsert(SqList &L,int dk){
int i,j;
for(i=dk+1;i<=L.length;++i)
if(L.r[i].key<L.r[i-dk].key){L.r[0]=L.r[i];
for(j=i-dk;j>0&& L.r[0].key<L.r[j].key;j-=dk)
L.r[j+dk]=L.r[j];
L.r[j+dk]=L.r[0];
}
}
void show(SqList L){
int i;
for(i=1;i<=L.length;i++)
cout<<L.r[i].key<<" ";
cout<<endl;
}
// t 趟希尔排序
void ShellSort(SqList &L,int dt[],int t){
int k;
for(k=0;k<t;++k){ShellInsert(L,dt[k]);
show(L);
}
}
void Create_Sq(SqList &L){
int i,n;
cout<<"数据个数:";
cin>>n;
cout<<"待排序的数据:";
for(i=1;i<=n;i++){cin>>L.r[i].key;
L.length++;
}
}
int main(){
SqList L;
L.r=new ElemType[MAXSIZE+1];
L.length=0;
Create_Sq(L);
int i,t;
int *dt=new int[MAXSIZE];
cout<<"总趟数:";
cin>>t;
for(i=0;i<t;i++){
cout<<"第"<<i+1<<"趟增量:";
cin>>dt[i];
}
cout<<"希尔排序:\n";
ShellSort(L,dt,t);
}
正文完