fork download
  1. #include <stdio.h>
  2.  
  3. #define N 20
  4.  
  5. int main()
  6. {
  7. int a[N] = {
  8. 45, 12, 78, 3, 56,
  9. 29, 91, 17, 64, 8,
  10. 33, 70, 1, 52, 86,
  11. 24, 39, 95, 11, 60
  12. };
  13.  
  14. int i, j, gap, temp;
  15.  
  16. printf("排序前的数据:\n");
  17.  
  18. for(i = 0; i < N; i++)
  19. {
  20. printf("%d ", a[i]);
  21. }
  22.  
  23. // 希尔排序
  24. for(gap = N / 2; gap > 0; gap /= 2)
  25. {
  26. for(i = gap; i < N; i++)
  27. {
  28. temp = a[i];
  29. j = i - gap;
  30.  
  31. while(j >= 0 && a[j] > temp)
  32. {
  33. a[j + gap] = a[j];
  34. j = j - gap;
  35. }
  36.  
  37. a[j + gap] = temp;
  38. }
  39. }
  40.  
  41. printf("\n\n希尔排序后的数据:\n");
  42.  
  43. for(i = 0; i < N; i++)
  44. {
  45. printf("%d ", a[i]);
  46. }
  47.  
  48. printf("\n");
  49.  
  50. return 0;
  51. }
Success #stdin #stdout 0s 5348KB
stdin
Standard input is empty
stdout
排序前的数据:
45 12 78 3 56 29 91 17 64 8 33 70 1 52 86 24 39 95 11 60 

希尔排序后的数据:
1 3 8 11 12 17 24 29 33 39 45 52 56 60 64 70 78 86 91 95