Chinaunix首页 | 论坛 | 博客
  • 博客访问: 22766
  • 博文数量: 3
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 58
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-19 12:19
文章分类

全部博文(3)

文章存档

2014年(3)

我的朋友

分类: C/C++

2014-09-22 21:38:17


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <time.h>

  5. #define N 15

  6. typedef void (*fp_sort)(int a[], int n);

  7. // 直接插入排序
  8. void insert_sort(int a[], int n)
  9. {
  10.     int i;

  11.     for (i = 1; i < n; i++) {
  12.         if (a[i] < a[i-1]) {
  13.             int j = i - 1;
  14.             int x = a[i];
  15.             while (x < a[j] && (j >= 0)) {
  16.                 a[j+1] = a[j];
  17.                 j--;
  18.             }
  19.             a[j+1] = x;
  20.         }
  21.     }
  22. }

  23. // 希尔排序
  24. void shell_sort(int a[], int n)
  25. {
  26.     int dk = n/2;
  27.     int i, j;
  28.     while (dk >= 1) {

  29.         for (i = 0; i < dk; i++) {
  30.             for (j = i + dk; j < n; j = j + dk) {
  31.                 int k = j;
  32.                 int x = a[k];
  33.                 while ((k >= dk) && (x < a[k-dk])) {
  34.                     a[k] = a[k-dk];
  35.                     k = k - dk;
  36.                 }
  37.                 a[k] = x;
  38.             }
  39.         }

  40.         dk = dk/2;
  41.     }
  42. }

  43. // 简单选择排序
  44. void select_sort(int a[], int n)
  45. {
  46.     int min, key;
  47.     int i, j;

  48.     for (i = 0; i < n; i++) {    
  49.         min = a[i];

  50.         for (j = i+1; j < n; j++)
  51.             if (min > a[j]) {
  52.                 min = a[j];
  53.                 key = j;
  54.             }

  55.         // a[key] = min
  56.         a[key] = a[i];
  57.         a[i] = min;
  58.     }

  59. }

  60. // 冒泡排序
  61. void bubble_sort(int a[], int n)
  62. {
  63.     int i, j, tmp;
  64.     for (i = n-1; i > 0; i--) {    
  65.         for (j = 0; j < i; j++) {
  66.             if (a[j] > a[j+1]) {
  67.                 tmp = a[j+1];
  68.                 a[j+1] = a[j];
  69.                 a[j] = tmp;
  70.             }

  71.         }
  72.     }
  73. }

  74. // 快速排序
  75. int split(int a[], int low, int high)
  76. {    
  77.     int i = low, j = high;
  78.     int key = a[i];

  79.     while (i != j) {
  80.         while (a[j] > key) {
  81.             j--;
  82.             if (i == j)
  83.                 goto out;
  84.         }
  85.         a[i] = a[j];
  86.         a[j] = key;
  87.         i++;
  88.         if (i == j)
  89.             goto out;
  90.         while (a[i] < key) {
  91.             i++;
  92.             if (i == j)
  93.                 goto out;
  94.         }
  95.         a[j] = a[i];
  96.         a[i] = key;
  97.         j--;
  98.     }

  99. out:
  100.     return i;
  101. }

  102. void quick_rec(int a[], int low, int high)
  103. {
  104.     if (low < high) {
  105.         int key = split(a, low, high);
  106.         quick_rec(a, low, key-1);
  107.         quick_rec(a, key+1, high);
  108.     }
  109. }

  110. void quick_sort(int a[], int n)
  111. {
  112.     quick_rec(a, 0, n-1);
  113. }


  114. int main()
  115. {
  116.     int i;
  117.     int a[N];
  118.     fp_sort fun_p;        //

  119.     // 产生 1 ~ 100 的随机数
  120.     srand(time(0));
  121.     for (i = 0; i < N; i++)
  122.         a[i] = rand()%100;

  123.     for (i = 0; i < N; i++)
  124.         printf("%d ", a[i]);

  125.     fun_p = bubble_sort;
  126.     printf("\nbubble_sort:\t");
  127.     (*fun_p)(a, N);
  128.     for (i = 0; i < N; i++)
  129.         printf("%d ", a[i]);

  130.     return 0;
  131. }

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

上一篇:一次学以致用的尝试

下一篇:没有了

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