#include<stdio.h>
#include<stdlib.h>
typedef int KeyType;
typedef struct {
KeyType *elem; /*elem[0]一般作哨兵或缓冲区*/
int Length;
}SqList;
void CreatSqList(SqList *L);/*待排序列建立,由裁判实现,细节不表*/
void ShellInsert(SqList L,int dk);
void ShellSort(SqList L);
int main()
{
SqList L;
int i;
CreatSqList(&L);
ShellSort(L);
for(i=1;i<=L.Length;i++)
{
printf("%d ",L.elem[i]);
}
return 0;
}
void ShellSort(SqList L)
{
/*按增量序列dlta[0…t-1]对顺序表L作Shell排序,假设规定增量序列为5,3,1*/
int k;
int dlta[3]={5,3,1};
int t=3;
for(k=0;k<t;++k)
ShellInsert(L,dlta[k]);
}
void ShellInsert(SqList L,int gap){
for (int i=gap;i<L.Length;++i){
for(int j=i+gap;j<L.Length+1;j+=gap){
if (L.elem[j]<L.elem[j-gap]){
int temp =L.elem[j];
int k=j-gap;
while (k>=1&&L.elem[k]>=temp){
L.elem[k+gap]=L.elem[k];
k-=gap;
}
L.elem[k+gap]=temp;
}
}
}
}
标签:ShellInsert,实现,void,elem,gap,int,希尔,SqList,排序 From: https://www.cnblogs.com/yankeqi/p/17496094.html