Chinaunix首页 | 论坛 | 博客
  • 博客访问: 370729
  • 博文数量: 100
  • 博客积分: 2500
  • 博客等级: 大尉
  • 技术积分: 1209
  • 用 户 组: 普通用户
  • 注册时间: 2011-04-15 21:24
文章分类

全部博文(100)

文章存档

2011年(100)

分类: C/C++

2011-04-20 16:02:27

  1. #include <stdio.h>

  2. void myPrint(int *A, int len);
  3. void mySwap(int *A, int *B);
  4. void shellPass(int *A, int d, int len);
  5. void shellSort(int *A, int len);

  6. int
  7. main(void)
  8. {
  9.         int a[] = {1,3,5,7,9,0,2,4,6,8,0};
  10.         int len = sizeof(a)/sizeof(a[0]);

  11.         myPrint(a, len);
  12.         shellSort(a, len);
  13.         myPrint(a, len);

  14.         return (0);
  15. }

  16. void
  17. myPrint(int *A, int len)
  18. {
  19.         printf("Enter: ");
  20.         int i = 0;
  21.         for (i = 0; i < len; i++) {
  22.                 printf("%d,",*(A+i));
  23.         }
  24.         printf("\n");
  25. }

  26. void
  27. mySwap(int *A, int *B)
  28. {
  29.         int tmp;
  30.         tmp = *A;
  31.         *A = *B;
  32.         *B = tmp;
  33. }

  34. void
  35. shellPass(int *A, int d, int len)
  36. {
  37.         int i, j, k;
  38.         int tmp;
  39.         for (i = 0; i < d; i++) {
  40.                 for (j = d*(i+1); j < len; j += d) {
  41.                         tmp = *(A+j);
  42.                         k = j-d;
  43.                         while (k >= i && *(A+k) > tmp) {
  44.                                 *(A+k+d) = *(A+k);
  45.                                 k -= d;
  46.                         }
  47.                         *(A+k+d) = tmp;
  48.                 }
  49.         }
  50. }

  51. void
  52. shellSort(int *A, int len)
  53. {
  54.         if (len <= 1)
  55.                 return ;

  56.         int inc = len;
  57.         do {
  58.                 inc = inc/3 + 1;
  59.                 shellPass(A, inc, len);
  60.         } while (inc > 1);
  61. }
稳定性:非稳定
T(n): O(nl.25,1.6n1.25)
S(n): O(1)


阅读(1214) | 评论(0) | 转发(0) |
0

上一篇:选择排序

下一篇:堆排序

给主人留下些什么吧!~~