Chinaunix首页 | 论坛 | 博客
  • 博客访问: 87333
  • 博文数量: 60
  • 博客积分: 4002
  • 博客等级: 中校
  • 技术积分: 645
  • 用 户 组: 普通用户
  • 注册时间: 2010-03-17 18:11
文章分类

全部博文(60)

文章存档

2011年(60)

我的朋友

分类: LINUX

2011-01-02 13:24:26

    插入排序:
    每次将一个待排序的记录,按其关键字大小插入到前面已经排好序的子文件中的适当位置,直到全部记录插入完成为止。

   
  1. #include <stdio.h>

  2. typedef int (*CompareFunc)(void *a, void *b);

  3. int Func(int *a, int *b);

  4. void InsertionSort(int *x, int len, CompareFunc f);


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

  13.     printf("Before Insertion Sort:\n");
  14.     for(i = 0; i < num; i++) {
  15.         printf("%d,", a[i]);
  16.     }
  17.     printf("\n\n");

  18.     InsertionSort(a, num, (CompareFunc)Func);

  19.     printf("After Insertion Sort:\n");
  20.     for(i = 0; i < num; i++) {
  21.         printf("%d,", a[i]);
  22.     }
  23.     printf("\n");

  24.     return 0;
  25. }

  26. int Func(int *a, int *b)
  27. {
  28.     if(*a > *b) {
  29.         return 1;
  30.     } else if(*a == *b) {
  31.         return 0;
  32.     } else {
  33.         return -1;
  34.     }
  35. }


  36. void InsertionSort(int *x, int len, CompareFunc f)
  37. {
  38.     if(len > 1) {
  39.         int i, j, key;

  40.         for(j = 1; j < len; j++) {
  41.             key = *(x+j);
  42.             i = j - 1;

  43.             while((i >= 0) && (f((x+i), &key)) == 1) {
  44.                 *(x+i+1) = *(x+i);
  45.                 --i;
  46.             }

  47.             *(x+i+1) = key;
  48.         }
  49.     }

  50.     return ;
  51. }

  52. /*
  53.  * Initialization: We start by showing that the loop invariant holds before the
  54.  *         first loop iteration, when j = 2. The subarray a[1...j-1], therefor
  55.  *        consists of just the single element a[1], which is in fact the
  56.  *        original element in a[1], Moreover, this subarray is sorted, which
  57.  *        shows that the loop invariant holds prior to the first iteration of
  58.  *        the loop
  59.  * Maintenance: Next we tackle the second property: showing that each iteration maintains
  60.  *    the loop invariant. Informally, the body of the outer for loop works by moving
  61.  *    a[j-1],a[j-2],a[j-3],and so on by one position to the right until the proper
  62.  *     positon for a[j] is found, at which point the value of a[j] is inserted.
  63.  *    A more formal treatment of second property would require us to stat and show a
  64.  *    loop invariant for the "inner" while loop. At this point, however, we prefer not
  65.  *     to get bogged down in such formalism, and so we reply on our informal analysis
  66.  *    to show that the second property holds for the outer loop.
  67.  * Termination: Finally, we examine what happens when loop terminates. For insertion sort,
  68.  *    the outer for loop ends when j exceeds n, when j = n + 1. Substituting n+1 for j
  69.  *    in the wording fo loop invariant, we have that the subarray a[1..n], consisits
  70.  *     of the elements of the elements original in a[1...n], but in sorted order. But
  71.  *    the subarray a[1...n] is the entire array. Hence, the entire array is sorted,
  72.  *    which means that the algorithm is correct.
  73.  */
注:平均时间复杂度为O(n2),稳定。


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

上一篇:mem

下一篇:归并排序

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