希尔排序(shell sort)

http://zh.wikipedia.org/wiki/%E5%B8%8C%E5%B0%94%E6%8E%92%E5%BA%8F


#include <stdio.h>
#include <stdlib.h>

int shell_sort(int num_array[], int num)
{
     int i, j, temp; 
     int gap = 0;

     while (gap<=num)
     {
          gap = gap * 3 + 1;
     } 
     
     while (gap > 0) 
     {
         for ( i = gap; i < num; i++ )
         {
             j = i - gap;
             temp = num_array[i];             
             while (( j >= 0 ) && ( num_array[j] > temp ))
             {
                 num_array[j + gap] = num_array[j];
                 j = j - gap;
             }
             num_array[j + gap] = temp;
         }
         gap = ( gap - 1 ) / 3;
     }    
     return 0;
 }

void main() 
{
    int i;
    int num_array[20];

    srand(0);
    printf("\r\n init: ");
    for(i = 0; i < 20; i++) {
        num_array[i] = rand()%1000;
        printf("%d ", num_array[i]);
    }

    shell_sort(num_array, 20);
    printf("\r\n shell sort: ");
    for(i = 0; i < 20; i++) {
        printf("%d ", num_array[i]);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值