王道数据结构 (17) 希尔排序

代码:

/*
 * @Author: your name
 * @Date: 2020-07-31 15:22:01
 * @LastEditTime: 2020-08-18 13:51:39
 * @LastEditors: your name
 * @Description: In User Settings Edit
 * @FilePath: C-data-structuremain.c
 */

#include <stdio.h>
#define N 10
void ShellSort(int num[], int len)
{
  int i, j, temp, step;
  for (step = len / 2; step >= 1; step /= 2) //步长间隔每次减半
    for (i = step; i < len; i += step)       //按步长遍历数组
    {
      //插入排序过程
      if (num[i] < num[i - step])
      {
        temp = num[i];
        for (j = i - step; num[j] > temp; j -= step)
          num[j + step] = num[j];
        num[j + step] = temp;
      }
    }
}
int main()
{
  int num[N] = {9, 8, 7, 6, 5, 4, 3, 2, 1, 0};
  ShellSort(num, N);
  for (int i = 0; i < N; i++)
    printf("%d ", num[i]);
  printf("
");
  return 0;
}

运行:

转发: https://blog.nowcoder.net/n/12bd2f64a468421b9ce379125c5b626b

代码仓库:

https://gitee.com/guangzhou110/kingcraft_data_structure

原文地址:https://www.cnblogs.com/guangzhou11/p/13523274.html