插入排序之希尔排序

 1 void shell_sort()
 2 {
 3     unsigned char i=0,j=0,gap;
 4     unsigned char arr[10] = {4,1,3,9,6,2,8,5,0,7};
 5     unsigned char len = sizeof(arr);
 6     unsigned char temp;
 7     char k;
 8     gap = len;
 9     while(gap)
10     {
11         gap /= 2;
12         /*共gap个数组,对每一组都进行直接插入排序*/
13         for(i = 0; i < gap; i++)
14         {
15             /*如果后值小于前值,则寻找后值位置*/
16             for( j = i+gap; j < len; j += gap)
17             {
18                 if(arr[j] < arr[j - gap])
19                 {
20                     temp = arr[j];
21                     k = j-gap;
22                     while(k >= 0 && arr[k] >temp)
23                     {
24                         arr[k + gap] = arr[k];
25                         k -= gap;
26                     }
27                     arr[k + gap] = temp;
28                 }                
29             }
30         }
31     }
32     
33     for(i=0;i<10;i++)
34     {
35         printf("%d\n",arr[i]);
36     }
37 }

 

posted @ 2023-08-28 16:49  njit-sam  阅读(9)  评论(0编辑  收藏  举报