希尔排序

// 希尔排序算法思想:缩小增量gap->划分序列->将每个子序列排序
#include < stdio.h >
void  shellsort( int  k[], int  n)
{
    
int  i,j,flag,gap = n;
    
int  tmp;
    
while (gap > 1 )
    {
        gap
= gap / 2 ;
        
do {
            flag
= 0 ;
            
for (i = 1 ;i <= n - gap;i ++ )
            {
                j
= i + gap;
                
if (k[i] < k[j])
                {
                    tmp
= k[i];
                    k[i]
= k[j];
                    k[j]
= tmp;
                    flag
= 1 ;
                }
            }
        }
while (flag != 0 );
    }
}

int  main()
{
    
int  i,a[ 11 ] = { - 111 , 2 , 5 , 6 , 3 , 7 , 8 , 0 , 9 , 12 , 1 };
    printf(
" The orginal data array is\n " );
    
for (i = 1 ;i <= 10 ;i ++ )
        printf(
" %d  " ,a[i]);

    shellsort(a,
10 );

    printf(
" \nThe result of Shell's sorting for the array is\n " ); // 从大到小排序
     for (i = 1 ;i <= 10 ;i ++ )
        printf(
" %d  " ,a[i]);
    
return   0 ;
}

你可能感兴趣的:(希尔排序)