1 #include<stdio.h> 2 #include<stdlib.h> 3 4 typedef struct 5 { 6 int *data; 7 int length; 8 }Sqlist; 9 10 11 /*顺序表的初始化*/ 12 void InitList(Sqlist &L, int l) 13 { 14 L.data = (int*)malloc((l+1)*sizeof(int)); 15 L.length = 0; 16 } 17 18 void CreateLList(Sqlist &L, int *a, int l) 19 { 20 L.length = 1; 21 for(int i=1; i<=l; i++) 22 { 23 L.data[i] = a[i-1]; 24 L.length++; 25 } 26 } 27 28 29 /* 30 实现希尔排序(缩小增量排序) 31 data[]增量序列 32 */ 33 void ShellInsert(Sqlist &L, int dk) 34 { 35 int j, i; 36 for(i = dk + 1; i<L.length; i++) 37 { 38 if (L.data[i]<L.data[i-dk]) 39 { 40 L.data[0] = L.data[i]; 41 for (j = i-dk; j > 0 && (L.data[0] < L.data[j]); j-=dk) 42 { 43 L.data[j+dk] = L.data[j]; 44 } 45 L.data[j+dk] = L.data[0]; 46 } 47 } 48 } 49 50 void ShellSort(Sqlist &L, int dlta[], int t) 51 { 52 for (int k = 0; k < t; k++) 53 { 54 ShellInsert(L,dlta[k]); //一趟增量为dlta[k]的插入排序 55 } 56 } 57 58 59 void DisplayList(Sqlist L) 60 { 61 for(int i = 1; i<L.length; i++) 62 { 63 printf("%d ",L.data[i]); 64 } 65 66 printf(" "); 67 } 68 69 int main(int argc, char const *argv[]) 70 { 71 Sqlist L; 72 int a[] = {27,13,76,97,65,38,49}; 73 int dlta[] = {5,3,1}; //增量序列(增量序列中的值没有除1之外的公因子,并且最后一个增量值必须是1) 74 int t = 3; //t为排序趟数 1<=t<=log(n+1) n标识要进行排序的序列中元素的个数 75 int l = sizeof(a)/sizeof(a[1]); 76 77 InitList(L,l); 78 CreateLList(L,a,l); 79 ShellSort(L,dlta,t); 80 DisplayList(L); 81 return 0; 82 }