希尔排序,基于严本 #include "stdio.h" #include "conio.h" #include "ctype.h" #include "stdlib.h" #define MAXSIZE 100 #define LT(a,b) ((a)<(b)) typedef int keytype; //以整数作为排序对象 typedef char* infotype; typedef struct { keytype key; infotype otherinfo; }redtype; typedef struct { redtype r[MAXSIZE+1]; int length; }SqList; //待排数据项 void Input(SqList *L){ //输入数据 int i=0; int count; //数据个数 scanf("%d",&count); while(i<count) scanf("%d",&L->r[++i].key); L->length=count; }//Input void ShellInsert(SqList *L,int dk){ //对同属于一组的数进行排序 int i,j; for(i=dk+1;i<=L->length;i++){ if(LT(L->r[i].key,L->r[i-dk].key)){ //需要将L->r[i]插入到前面 L->r[0]=L->r[i]; //暂存 L->r[i]=L->r[i-dk]; for(j=i-2*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]; }//if }//for }//ShellInsert void ShellSort(SqList *L){ //排序主函数 int *dlta,i,n,j; dlta=(int*)malloc(L->length*sizeof(int)); for(n=L->length/2,i=0;n;n=n/2) //增量序列 dlta[i++]=n; for(j=0;j<i;j++) ShellInsert(L,dlta[j]); for(i=1;i<=L->length;i++) printf("%d ",L->r[i].key); }//ShellSort void main(){ SqList L; Input(&L); //输入 ShellSort(&L); }