几种排序算法

来源:互联网 发布:淘宝做分销怎么样 编辑:程序博客网 时间:2024/05/01 20:55

 


点击(此处)折叠或打开

  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. }

<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(142) | 评论(0) | 转发(3) |
0

上一篇:关于C和C++输入缓冲区的问题

下一篇:brk()和sbrk()的简单练习

相关热门文章
  • 网站优化之title、keywords、d...
  • 合同撤销权的内容
  • 潜水泵水流的控制的调节方法...
  • seo两种操作手法已经过时...
  • 烂泥:apache性能测试工具ab的...
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • sql relay的c++接口
  • 谁能够帮我解决LINUX 2.6 10...
  • 现在的博客积分不会更新了吗?...
  • shell怎么读取网页内容...
  • ssh等待连接的超时问题...
  • curl: (56) Recv failure: Con...
给主人留下些什么吧!~~
原创粉丝点击