Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1668151
  • 博文数量: 1493
  • 博客积分: 38
  • 博客等级: 民兵
  • 技术积分: 5834
  • 用 户 组: 普通用户
  • 注册时间: 2009-08-19 17:28
文章分类

全部博文(1493)

文章存档

2016年(11)

2015年(38)

2014年(137)

2013年(253)

2012年(1054)

2011年(1)

分类:

2012-11-20 16:17:40

原文地址:几种排序算法 作者:tiger54


 


点击(此处)折叠或打开

  1. /*
  2. **选择排序,10000个数据27.32秒
  3. */
  4. #include using namespace std;
  5. void sort(int* a,const int len)
  6. {
  7.     for(int i=0;i<len;i++)
  8.     {
  9.        int temp = i;
  10.        for(int j=i;j<len;j++)
  11.        {
  12.            if(a[temp] < a[j]) {
  13.               temp = j;
  14.             }
  15.        }
  16.        int t = a[temp];
  17.        a[temp] = a[i];
  18.        a[i] = t;
  19.      }
  20. }
  21. /*
  22. **插入排序,10000个数据21.13秒
  23. */
  24. void sort(int* a,const int len)
  25. {
  26.   int temp,j;
  27.   for(int i=1;i=0&&a[j]<temp;j--)
  28.   {
  29.      a[j+1] = a[j];
  30.   }
  31.   a[j+1] = temp;
  32.   }
  33. }
  34. /*
  35. **快速排序,10000个数据0.23秒!
  36. */
  37. void sort(int* a,const int len)
  38. {
  39.    if(len<=0) return;
  40.    int L = 0;
  41.    int R = len-1;
  42.    int temp = a[L];
  43.     while(L<R) {
  44.        while( L=a[R] ) R--;
  45.        a[L] = a[R];
  46.        while( L<R && temp<=a[L] )L++;
  47.        a[R] = a[L];
  48.      }
  49.      a[L] = temp;
  50.      sort(a,L);
  51.      sort(a+L+1,len-1-L);
  52. }
  53. /*
  54. **测试代码
  55. */
  56. #include
  57. #include
  58. #include
  59. #include
  60. using namespace std;
  61. int main(int argc,char* argv[])
  62. {
  63.     srand(time(0));
  64.     int a[NUM] ;
  65.     for(int i=0;i<NUM;i++)
  66.     {
  67.        a[i] = rand()%NUM;
  68.     }
  69.     for(int i=0;i<20;i++)
  70.     {
  71.         cout << a[i] << " ";
  72.      }
  73.      cout<< "..." << endl;
  74.      clock_t beg = clock();
  75.      sort(a,a+NUM);
  76.      clock_t end = clock();
  77.      for(int i=0;i<20;i++)
  78.      {
  79.         cout << a[i] << " ";
  80.      }
  81.      cout<< "..." << endl;
  82.      cout << "共用时 " << (end-beg)*1.0/CLOCKS_PER_SEC << " 秒" << endl;
  83.      return 0;
  84. }

阅读(163) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~