算法与数据结构Day01

希尔排序的实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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 = 1 + dk; i <= L.Length; ++i)
    {
        if(L.elem[i] < L.elem[i-dk])
        {
            L.elem[0] = L.elem[i];
            int x = i -dk;
            for(; L.elem[x]>L.elem[0] && x>0;x-=dk)
            {
                L.elem[x+dk] = L.elem[x];
            }
            L.elem[x+dk] = L.elem[0];
        }
    }
         
}

  

posted @   ME社长  阅读(5)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· 三行代码完成国际化适配,妙~啊~
· .NET Core 中如何实现缓存的预热?
· 如何调用 DeepSeek 的自然语言处理 API 接口并集成到在线客服系统
点击右上角即可分享
微信分享提示