经典排序算法之冒泡排序
来源:互联网 发布:excel省市数据 编辑:程序博客网 时间:2024/05/18 02:43
基本思路:比较表中的相邻元素,如果他们是逆序的话就交换他们的位置。重复多次以后,最终最大
的元素就沉到列表的最后一个位置,第二遍操作将第二大的元素沉下去,这样一直做,知道n-1遍以后,该列表就排好序了。
代码如下:
包括比较次数
运行结果如下:
[root@localhost ~]# ./a.out
4
4 3 2 1
6
1
2
3
4
去哪笔试题(2016.09.08)
运行结果:
[root@bogon ~]# ./a.out
Coder,Coder
2
i am a coder
1
Coder
1
[root@bogon ~]#
的元素就沉到列表的最后一个位置,第二遍操作将第二大的元素沉下去,这样一直做,知道n-1遍以后,该列表就排好序了。
代码如下:
点击(此处)折叠或打开
- #include<stdio.h>
- #include<malloc.h>
- #include<iostream>
- using namespace std;
- //#define swap(x,y) (x ^= y,y ^= x,x ^= y)
- int minTime(int *a,int n){
- if (a ==NULL || n == 0)
- return 0;
- int i = 0,j = 0;
- int count = 0;
- for (i = 0;i < n-1;i++)
- {
- for (j = 0;j < n-i-1;j++)
- {
- if (a[j] > a[j+1])
- {
- swap(a[j],a[j+1]);
- count++;
- }
-
- }
- }
- return count;
- }
- int main()
- {
-
- int len = 0;
- while (cin >> len)
- {
- int *a = (int *)malloc(sizeof(int)*len);
- int i = 0;
- for (i = 0;i < len;i++)
- {
- cin >> a[i];
- }
- int result = minTime(a,len);
- printf("%d\n",result);
- for (i = 0;i < len;i++)
- {
- printf("%d\n",a[i]);
- }
- free(a);
- }
- return 0;
- }
运行结果如下:
[root@localhost ~]# ./a.out
4
4 3 2 1
6
1
2
3
4
去哪笔试题(2016.09.08)
点击(此处)折叠或打开
- #include<iostream>
- #include<vector>
- using namespace std;
- class Coder {
- public:
- vector<string> findCoder(vector<string> A, int n) {
- vector<string> result;
- vector<int> resultInt;
- //int count = 0;
- vector<string>::iterator iter = A.begin();
- for (;iter != A.end();iter++)
- {
- int j =0;
- string s = *iter;
- int count = 0;
- for (int i = 0;i < (*iter).length() && i+4 < (*iter).length();)
- {
- 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')
- {
- i = i+5;
- count++;
- }
- else
- i = i+1;
- }
- //cout << count << endl;
- result.push_back(*iter);
- resultInt.push_back(count);
- }
- for (int i = 0;i < n-1;i++)
- {
- for (int j = 0;j < n-i-1;j++)
- {
- if (resultInt[j] < resultInt[j+1])
- {
- swap(resultInt[j],resultInt[j+1]);
- swap(result[j],result[j+1]);
- }
- }
- }
- for (int i = 0;i < n;i++)
- {
- cout << result[i]<< endl;
- cout << resultInt[i]<< endl;
- }
- return result;
- }
- };
- int main()
- {
- Coder coder;
- string str[3] = {"i am a coder","Coder,Coder","Coder"};
- vector<string> A(&str[0],&str[3]);
- coder.findCoder(A,3);
- return 0;
- }
运行结果:
[root@bogon ~]# ./a.out
Coder,Coder
2
i am a coder
1
Coder
1
[root@bogon ~]#
0
上一篇:linux进程调度
下一篇:qq原理
相关热门文章
- test123
- 编写安全代码——小心有符号数...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- 彻底搞定C语言指针详解-完整版...
给主人留下些什么吧!~~
评论热议
阅读全文
0 0
- 经典排序算法之冒泡排序算法
- 经典算法之冒泡排序
- 经典算法之冒泡排序
- 经典算法之冒泡排序
- C--经典算法之排序---冒泡排序
- 经典算法学习:排序之冒泡排序
- 经典排序算法之冒泡排序
- 经典排序算法之冒泡排序
- 经典排序算法之冒泡排序
- 经典排序算法之冒泡排序
- 经典排序算法之------冒泡排序
- Java经典排序算法之冒泡排序
- 经典排序算法之冒泡排序
- 【排序算法】经典排序算法之冒泡排序
- JS经典算法之冒泡排序算法
- 冒泡---经典排序算法
- 经典算法---冒泡排序
- 经典算法---冒泡排序
- 【数据结构】中对n个数字右移k位
- 协同进程
- linux下C和shell调用的popen函数
- 对工厂模式的简单理解
- linux进程调度
- 经典排序算法之冒泡排序
- qq原理
- 添加系统调用
- list_for_each_entry
- 字符串的包含
- 字符串的排列组合
- NGINX实现IF语句里的AND,OR多重判断
- 经典排序算法之堆排序
- 经典排序算法之归并排序
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
歌尔股份股吧
歌尔股吧
潍坊歌尔
潍坊歌尔妹子于倩事件
山东歌尔
歌尔股份股
山东歌尔公司怎么样
歌尔集团
歌尔股票价格
山东歌尔公司
歌尔招聘
002241歌尔股份股吧
潍坊歌尔妹子于倩图片
潍坊歌尔宿舍照片
歌尔集团简介
山东歌尔电子有限公司怎么样
潍坊歌尔招聘
潍坊歌尔怎么样
山东歌尔电子有限公司
歌尔股票股吧
歌尔股份股吧东方财富
歌尔股份股价
山东潍坊歌尔电子厂
歌尔股票行情
潍坊歌尔绿城
姜斌歌尔
歌尔股份吧
歌尔吧
歌山
歌蒂梵
歌梵蒂
巧克力歌帝梵
歌梵帝
哥帝梵
歌帝梵世界排名第几
歌帝梵巧克力多少钱
歌帝梵和费列罗哪个好
gadiva
哥蒂凡
godiba
godvia