Chinaunix首页 | 论坛 | 博客
  • 博客访问: 175458
  • 博文数量: 63
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 69
  • 用 户 组: 普通用户
  • 注册时间: 2013-03-18 13:11
文章分类
文章存档

2013年(64)

我的朋友

分类: C/C++

2013-08-16 13:45:01

原文地址:笔试中常考的排序算法 作者:ygfinsight


点击(此处)折叠或打开

  1. #include <stdio.h>
  2. //打印函数
  3. void print(int a[])
  4. {
  5.     int i;
  6.     for(i=0;i<10;i++){
  7.         printf("%d ",a[i]);
  8.     }
  9.     printf("\n");
  10. }
  11. /*插入排序
  12.     思想:顺序的把待排序的数据元素按关键字的大小插入到已排序数据元素子集合适当的位置
  13.          子集合的数据元素个数从只有一个数据元素开始,逐次增大,当子集合大小最终和集合大小相同时,排序完毕
  14.     平均时间复杂度:O(n^2)
  15. */
  16. void insert_sort(int a[],int n)
  17. {
  18.     printf("fun::sizeof(a)=%d\n",sizeof(a));
  19.     int i;
  20.     int temp;//form 1 to the end
  21.     for(i=0;i<n-1;i++){
  22.         temp=a[i+1];
  23.         int j=i;
  24.         while(j>-1 && temp<a[j]){
  25.             a[j+1]=a[j];
  26.             j--;
  27.         }
  28.         a[j+1]=temp;
  29.     }
  30. }
  31. /*选择排序:
  32.         思想:从待排的数据元素集合中选取关键字最小的数据元素,
  33.         并放到数据元素集合的最前面,数据元素集合不断缩小,
  34.         当数据元素集合为空时,选择排序结束
  35. 直接选择排序的平均时间复杂度:O(n^2)
  36. 堆排序的时间复杂度:o(nlb^n)

  37. */
  38. //直接选择排序
  39. void select_insert(int a[],int n)
  40. {
  41.     int i,j,min;
  42.     int temp;
  43.     for (i = 0; i < n-1; i++)
  44.     {
  45.         min=i;//每次设集合中的第一个元素关键字最小
  46.         //
  47.         for(j=i+1;j<n;j++){
  48.             if(a[j]<a[min])
  49.                 min=j;
  50.         }
  51.         if (i!=min)
  52.         {
  53.             temp=a[i];
  54.             a[i]=a[min];
  55.             a[min]=temp;
  56.         }

  57.     }

  58. }
  59. /*
  60.     交换排序:主要是靠交换数据元素的位置进行排序
  61.     冒泡排序时间复杂度:O(n^2)
  62.     快速排序时间复杂度:O(nlb^n)

  63. */

  64. //冒泡排序
  65. void bubble_sort(int a[],int n)
  66. {
  67.     int i,j;
  68.     int flag=1;
  69.     int temp;
  70.     for (i = 0; i < n && flag==1; ++i)
  71.     {
  72.         flag=0;
  73.         for (j = 0; j <n-i; ++j)
  74.         {
  75.             if (a[j]>a[j+1])
  76.             {
  77.                 temp=a[j];
  78.                 a[j]=a[j+1];
  79.                 a[j+1]=temp;

  80.                 flag=1;

  81.             }
  82.         }
  83.     }


  84. }
  85. //快速排序
  86. /*
  87.     快速排序基本思想
  88.         设数组元素a中存放了n个数据元素,low为数组的低端下标,
  89.         high为数组的高端下标,从a中任取一个元素作为标准,调整数组a中各个
  90.         元素的位置,使排在标准元素前面的元素的关键字都小于标准元素的的关键
  91.         字,排在标准元素后面元素的关键字都大于等于标准元素的关键字。这样,标准
  92.         元素放在了该放的位置,另外将这个数组分为了两个子数组。对这两个子数组
  93.         再进行一样的递归快速排序,递归结束条件是low<high
  94.     快速排序平均时间复杂度:O(nlb^n)
  95. */

  96. void quick_sort(int a[],int low,int high)
  97. {
  98.     int i=low,j=high;
  99.     int temp=a[low];
  100.     while(i<j){
  101.         while(i<j&&a[j]>a[low]) j--;//在数组右端扫描
  102.         if (i<j)
  103.         {
  104.             a[i]=a[j];
  105.             i++;
  106.         }
  107.         while(i<j && a[i]<a[low]) i++;//在数组左端扫描
  108.         if (i<j)
  109.         {
  110.             a[j]=a[i];
  111.             j--;
  112.         }
  113.     }
  114.     a[i]=temp;
  115.     if (low<i) quick_sort(a,low,i-1);
  116.     if (i<high) quick_sort(a,j+1,high);
  117.     
  118. }
  119. int main()
  120. {
  121.     int a[10]={1,2,4,5,3,6,8,7,9,0};
  122.     //printf("main::sizeof(a)=%d\n",sizeof(a));

  123.     //insert_sort(a,10);
  124.     //select_insert(a,10);
  125.     //bubble_sort(a,10);
  126.     quick_sort(a,0,9);
  127.     print(a);
  128.     return 0;
  129. }

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

上一篇:linux ppp pppoe

下一篇:关于copy_from_user函数

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