Chinaunix首页 | 论坛 | 博客
  • 博客访问: 188391
  • 博文数量: 49
  • 博客积分: 635
  • 博客等级: 中士
  • 技术积分: 410
  • 用 户 组: 普通用户
  • 注册时间: 2011-08-25 12:58
文章分类

全部博文(49)

文章存档

2012年(9)

2011年(40)

分类:

2011-08-26 12:40:26

原文地址:希尔排序shellsort的C实现 作者:Bean_lee

     shell排序是一种比较高效的排序算法,虽然理论上不是NlogN的算法复杂度。但是不影响shell的速度。

     快速排序quicksort存在两个问题,一个是递归实现,有可能栈溢出。第二个问题是,快速排序在极端情况下,算法复杂度有可能退化成(N^2)。

     如果不确定使用快速排序是否正确的场合,使用shell排序是个不错的选择,因为shell排序,不需要对输入的数据进行太多的分析调查。

     shell排序的算法复杂度,与shellsort中的步长有很大的关系。选择一个好的步长对shellsort很关键。shellsort的始祖Shell提出算法时给出的步长序列是 (2^n), 后续的研究证明是很烂的一个步长序列。

     简单的步长序列有:
1)  3*n+1   即(1,4,13,40,121,364,1093,3280……)           算法复杂度低于O(N^1.5)

2)  4^(i+1) + 3*2^(i) + 1  即(1,8,23,77,281,1073,4193……) 算法复杂度低于O(N^(4/3))


OK,理论部分到此结束,下面给出代码。
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3. #include<time.h>

  4. void shellsort(int array[],int l,int r)
  5. {
  6.                 int i,j,hop;
  7.                 int curitem;
  8.                 for( hop = 1;hop <= (r-l)/9;hop=3*hop+1)
  9.                                 ;

  10.                 for(;hop>0;hop/=3)
  11.                 {
  12.                                 for(i = l+hop;i <= r ; i++)
  13.                                 {
  14.                                                 j = i;
  15.                                                 curitem= array[i];
  16.                                                 while( (j>=(l+hop)) && curitem < array[j-hop])
  17.                                                 {
  18.                                                                 array[j] = array[j-hop];
  19.                                                                 j = j-hop;
  20.                                                 }
  21.                                                 array[j] = curitem;
  22.                                 }
  23.                 }
  24. }


  25. int test_shellsort()
  26. {
  27.                 int i;
  28.                 int number = 50;

  29.                 int *array = malloc(number*sizeof(int));
  30.                 if(array == NULL)
  31.                 {
  32.                                 printf("malloc failed\n");
  33.                                 return -1;
  34.                 }


  35.                 printf("----------------------------------------before shell sort--------------\n");
  36.                 srand(time(NULL));
  37.                 for(i = 0;i<number;i++)
  38.                 {
  39.                                 array[i] = rand()%10000;
  40.                                 printf("\tarray[%6d] = %6d\n",i,array[i]);
  41.                 }

  42.                 printf("----------------------------------------after shell sort-----------------\n");

  43.                 shellsort(array,0,number-1);
  44.                 for(i = 0;i<number;i++)
  45.                 {
  46.                         printf("\tarray[%6d] = %6d\n",i,array[i]);
  47.                 }
  48.                 free(array);
  49.                 return 0;

  50. }
  51. int main()
  52. {
  53.                 test_shellsort();
  54.                 return 0;
  55. }


  1. ----------------------------------------before shell sort--------------
  2. array[ 0] = 8164
  3. array[ 1] = 4430
  4. array[ 2] = 6981
  5. array[ 3] = 5061
  6. array[ 4] = 6818
  7. array[ 5] = 9343
  8. array[ 6] = 4056
  9. array[ 7] = 4840
  10. array[ 8] = 449
  11. array[ 9] = 5046
  12. array[ 10] = 5614
  13. array[ 11] = 1229
  14. array[ 12] = 9509
  15. array[ 13] = 9703
  16. array[ 14] = 9677
  17. array[ 15] = 1856
  18. array[ 16] = 45
  19. array[ 17] = 4645
  20. array[ 18] = 5968
  21. array[ 19] = 9310
  22. array[ 20] = 7043
  23. array[ 21] = 5083
  24. array[ 22] = 4170
  25. array[ 23] = 7518
  26. array[ 24] = 4596
  27. array[ 25] = 4611
  28. array[ 26] = 1613
  29. array[ 27] = 5947
  30. array[ 28] = 6859
  31. array[ 29] = 5396
  32. array[ 30] = 6291
  33. array[ 31] = 1375
  34. array[ 32] = 6179
  35. array[ 33] = 9624
  36. array[ 34] = 6437
  37. array[ 35] = 2997
  38. array[ 36] = 5320
  39. array[ 37] = 493
  40. array[ 38] = 4190
  41. array[ 39] = 2121
  42. array[ 40] = 1891
  43. array[ 41] = 6156
  44. array[ 42] = 3350
  45. array[ 43] = 1401
  46. array[ 44] = 5859
  47. array[ 45] = 3027
  48. array[ 46] = 9609
  49. array[ 47] = 5905
  50. array[ 48] = 4025
  51. array[ 49] = 5577
  52. ----------------------------------------after shell sort-----------------
  53. array[ 0] = 45
  54. array[ 1] = 449
  55. array[ 2] = 493
  56. array[ 3] = 1229
  57. array[ 4] = 1375
  58. array[ 5] = 1401
  59. array[ 6] = 1613
  60. array[ 7] = 1856
  61. array[ 8] = 1891
  62. array[ 9] = 2121
  63. array[ 10] = 2997
  64. array[ 11] = 3027
  65. array[ 12] = 3350
  66. array[ 13] = 4025
  67. array[ 14] = 4056
  68. array[ 15] = 4170
  69. array[ 16] = 4190
  70. array[ 17] = 4430
  71. array[ 18] = 4596
  72. array[ 19] = 4611
  73. array[ 20] = 4645
  74. array[ 21] = 4840
  75. array[ 22] = 5046
  76. array[ 23] = 5061
  77. array[ 24] = 5083
  78. array[ 25] = 5320
  79. array[ 26] = 5396
  80. array[ 27] = 5577
  81. array[ 28] = 5614
  82. array[ 29] = 5859
  83. array[ 30] = 5905
  84. array[ 31] = 5947
  85. array[ 32] = 5968
  86. array[ 33] = 6156
  87. array[ 34] = 6179
  88. array[ 35] = 6291
  89. array[ 36] = 6437
  90. array[ 37] = 6818
  91. array[ 38] = 6859
  92. array[ 39] = 6981
  93. array[ 40] = 7043
  94. array[ 41] = 7518
  95. array[ 42] = 8164
  96. array[ 43] = 9310
  97. array[ 44] = 9343
  98. array[ 45] = 9509
  99. array[ 46] = 9609
  100. array[ 47] = 9624
  101. array[ 48] = 9677
  102. array[ 49] = 9703
  103. real 0m0.002s
  104. user 0m0.000s
  105. sys 0m0.004s


参考文献:1 算法:C语言实现
阅读(748) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~