排序
来源:互联网 发布:计算机的发展史软件 编辑:程序博客网 时间:2024/05/27 21:03
一、起泡排序
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <map>#include <cmath>#include <cctype>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef unsigned int uint;const ull mod = 1e9 + 7;const int INF = 0x7fffffff;const int maxn = 1e6 + 10;int n;int num[maxn];void bubbleSort(int low, int high);bool bubble(int low, int high);int main(){#ifdef __AiR_H freopen("in.txt", "r", stdin);#endif // __AiR_H scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &num[i]); } bubbleSort(0, n); for (int i = 0; i < n; ++i) { printf("%d ", num[i]); } return 0;}void bubbleSort(int low, int high){ while (!bubble(low, high--));}bool bubble(int low, int high){ bool sorted = true; while (++low < high) { if (num[low-1] > num[low]) { sorted = false; swap(num[low], num[low-1]); } } return sorted;}
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <algorithm>#include <queue>#include <vector>#include <stack>#include <map>#include <cmath>#include <cctype>using namespace std;typedef long long ll;typedef unsigned long long ull;typedef unsigned int uint;const ull mod = 1e9 + 7;const int INF = 0x7fffffff;const int maxn = 1e6 + 10;int n;int num[maxn];void bubbleSort(int low, int high);int bubble(int low, int high);int main(){#ifdef __AiR_H freopen("in.txt", "r", stdin);#endif // __AiR_H scanf("%d", &n); for (int i = 0; i < n; ++i) { scanf("%d", &num[i]); } bubbleSort(0, n); for (int i = 0; i < n; ++i) { printf("%d ", num[i]); } return 0;}void bubbleSort(int low, int high){ while (low < high) { high = bubble(low, high); }}int bubble(int low, int high){ int last = low; while (++low < high) { if (num[low-1] > num[low]) { last = low; swap(num[low], num[low-1]); } } return last;}
0 0
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- 排序
- java 多线程之Executors.newFixedThreadPool
- 事件处理机制之响应系统设置的事件(Configuration类)
- java中substring和indexof() 和lastindexof()
- 一款不错的html5网站模板案例代码下载,用于开发个人网站,兼容手机与PC
- Java List 通用排序工具类
- 排序
- 打印数组所有内容
- AJAX入门
- Java 文件读取
- Hadoop2.4.1 简单的用户手机流量统计的MapReduce程序(三)
- UIWebView与H5的交互
- 读取前几行
- 在github中托管项目
- mysql安装失败