练习5
作者:互联网
(1)今日安排
希尔排序的实现
题目要求:本题要求实现一趟希尔排序函数,待排序列的长度1<=n<=1000。
#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 dk){ for(int i=dk+1;i<=L.Length;i++){ int p=i; L.elem[0]=L.elem[p]; p-=dk; while(p>0&&L.elem[p]>L.elem[0]){ L.elem[p+dk]=L.elem[p]; p-=dk; } L.elem[p+dk]=L.elem[0]; } }
标签:typedef,dk,int,void,练习,elem,SqList 来源: https://www.cnblogs.com/qiuyutingyyds/p/15677387.html