经典排序算法之冒泡排序

来源:互联网 发布:excel省市数据 编辑:程序博客网 时间:2024/05/18 02:43
基本思路:比较表中的相邻元素,如果他们是逆序的话就交换他们的位置。重复多次以后,最终最大
的元素就沉到列表的最后一个位置,第二遍操作将第二大的元素沉下去,这样一直做,知道n-1遍以后,该列表就排好序了。
代码如下:

点击(此处)折叠或打开

  1. #include<stdio.h>
  2. #include<malloc.h>
  3. #include<iostream>
  4. using namespace std;
  5. //#define swap(x,y) (x ^= y,y ^= x,x ^= y)
  6. int minTime(int *a,int n){
  7.     if (a ==NULL || n == 0)
  8.         return 0;
  9.     int i = 0,j = 0;
  10.     int count = 0;
  11.     for (i = 0;i < n-1;i++)
  12.     {
  13.         for (j = 0;j < n-i-1;j++)
  14.         {
  15.             if (a[j] > a[j+1])
  16.             {
  17.                 swap(a[j],a[j+1]);
  18.                 count++;
  19.             }
  20.                 
  21.         }
  22.     }
  23.     return count;
  24. }
  25. int main()
  26. {
  27.     
  28.    int len = 0;
  29.    while (cin >> len)
  30.    {
  31.        int *a = (int *)malloc(sizeof(int)*len);
  32.        int i = 0;
  33.        for (i = 0;i < len;i++)
  34.        {
  35.             cin >> a[i];
  36.        }
  37.        int result = minTime(a,len);
  38.        printf("%d\n",result);
  39.        for (i = 0;i < len;i++)
  40.        {
  41.         printf("%d\n",a[i]);
  42.     }
  43.        free(a);
  44.    }
  45.     return 0;
  46. }
包括比较次数
运行结果如下:
[root@localhost ~]# ./a.out
4
4 3 2 1
6
1
2
3
4
去哪笔试题(2016.09.08)

点击(此处)折叠或打开

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. class Coder {
  5. public:
  6.     vector<string> findCoder(vector<string> A, int n) {
  7.           vector<string> result;
  8.     vector<int> resultInt;
  9.     //int count = 0;
  10.     vector<string>::iterator iter = A.begin();
  11.     for (;iter != A.end();iter++)
  12.     {
  13.         int j =0;
  14.         string s = *iter;
  15.         int count = 0;
  16.         for (int i = 0;i < (*iter).length() && i+4 < (*iter).length();)
  17.         {
  18.             if (toupper(s[i]) == 'C' && toupper(s[i+1]) == 'O' && toupper(s[i+2]) == 'D' && toupper(s[i+3]) == 'E' && toupper(s[i+4]) == 'R')
  19.             {
  20.                 i = i+5;
  21.                 count++;
  22.             }
  23.             else
  24.                 i = i+1;
  25.         }
  26.         //cout << count << endl;
  27.         result.push_back(*iter);
  28.         resultInt.push_back(count);
  29.     }
  30.     for (int i = 0;i < n-1;i++)
  31.     {
  32.         for (int j = 0;j < n-i-1;j++)
  33.         {
  34.             if (resultInt[j] < resultInt[j+1])
  35.             {
  36.                 swap(resultInt[j],resultInt[j+1]);
  37.                 swap(result[j],result[j+1]);
  38.             }
  39.         }
  40.     }
  41.     for (int i = 0;i < n;i++)
  42.     {
  43.         cout << result[i]<< endl;
  44.         cout << resultInt[i]<< endl;
  45.     }
  46.     return result;
  47.     }
  48. };
  49. int main()
  50. {
  51.     Coder coder;
  52.     string str[3] = {"i am a coder","Coder,Coder","Coder"};
  53.     vector<string> A(&str[0],&str[3]);
  54.     coder.findCoder(A,3);
  55.     return 0;
  56. }

运行结果:
[root@bogon ~]# ./a.out
Coder,Coder
2
i am a coder
1
Coder
1
[root@bogon ~]#  


<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>
阅读(161) | 评论(0) | 转发(0) |
0

上一篇:linux进程调度

下一篇:qq原理

相关热门文章
  • test123
  • 编写安全代码——小心有符号数...
  • 使用openssl api进行加密解密...
  • 一段自己打印自己的c程序...
  • 彻底搞定C语言指针详解-完整版...
给主人留下些什么吧!~~
原创粉丝点击