【排序】Shell Sort

 Why does it call Shell Sort? This time it's name has little to do with  the method it uses. In fact , it's alse call Decreasing incremental sorting algorithm (递减增量排序算法)

It's because of Donald Shell, who invented this method:  

divide List into servral column (first column = length/2)

but each time column get smaller, until column =1 (when column =1 , it become a real insert sort)

each insert sort , it's elements include each column instead of the whole List .

It's main feature : compare more but move less

 #include <stdio.h>

#include "type.h"
void ShellSort(SqList *L)
{
    
   int increase_step = 2
   //div List into several column , and each column use insert sort
    for(int column = L->length/increase_step; column >0; column=column/increase_step)
    {
            //insert sort of each column
        forint line = column; line < L->length; line++)
        {
            int com = L->data[line];
            int j= line - column;
            //in a step of column_num
            for( ; com < L->data[j] && j>=0 ; j-=column)
            {
            L->data[j+column]=L->data[j];
            }
            L->data[j+column] = com;
        }
        //for clear watch a List , output the List in column 
        for(int i = 0; i< L->length; i++)
        {
            if(0== i%column)
                printf("\n");
            printf("%d ", L->data[i]);
            
        }
    printf("\n\n");

    }
}
void printContent(SqList *L)
{
    for(int i = 0; i< L->length; i++)
    {
        printf("%d \t",L->data[i] );
    }
}
int main(void)
{
    SqList l ;
   
   l.data={17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1};
   l.length = 17;
    printContent(&l);
    printf("\n");
    ShellSort(&l);
    printContent(&l);
    printf("\n");

    return 0;
}


17     16     15     14     13     12     11     10     9     8     7     6     5     4     3     2     1   (first , original)  
(column set to 17/2 = 8, the following is the first sort result of each column. Note that each column is in order now)
1 8 7 6 5 4 3 2 
9 16 15 14 13 12 11 10 
17 

(column set to 8/2 = 4, the following is the first sort result of each column. Note that each column is in order now, again)
1 4 3 2 
5 8 7 6 
9 12 11 10 
13 16 15 14 
17 

(column set to 4/2 = 2, the following is the first sort result of each column. Note that each column is in order now)
1 2 
3 4 
5 6 
7 8 
9 10 
11 12 
13 14 
15 16 
17 

(in the end, this is just a insert sort)
1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
(final result)

1     2     3     4     5     6     7     8     9     10     11     12     13     14     15     16     17 

the most difficult point to understand is about increase_step , it set to length/2 at the first time . But it has some way to improve . refer to this.
原文地址:https://www.cnblogs.com/no7dw/p/2793859.html