冒泡排序(正宗点吧)
2014-03-24 23:40
197 查看
#include <iostream> #include <vector> using namespace std; void swap(int & a,int & b) { a=a+b; b=a-b; a=a-b; } vector<int> & bubbleSort(vector<int> & v) { int n=v.size(); int flag; for(auto i =0;i < n;i++) { flag =false; for(auto j=n-1;j >i;j--) { if(v[j-1] > v[j]) { swap(v[j-1],v[j]); flag =true; } } if(!flag) return v; } return v; } void print(vector<int> & v) { vector<int>::iterator pos; for(pos = v.begin();pos!=v.end();pos++) cout<<*pos<<" "; cout<<endl; } void main() { const int n=100; vector<int> v; v.reserve(n); for(int i =0;i<n;i++) v.push_back(rand()%100); print(v); //print(bubbleSort1(v)); print(bubbleSort(v)); system("pause"); }
相关文章推荐
- 正宗的冒泡排序
- C++冒泡排序(正宗版)
- 初级版、正宗版、升级版冒泡排序;简单选择排序;直接插入排序;希尔排序;堆排序;递归法 、非递归法归并排序;快速排序; 快速排序优化算法
- 正宗 ~ 冒泡排序
- 基本排序之冒泡排序
- 排序算法 之 冒泡排序 BubbleSort
- 复习一下冒泡排序,做水题,好像我就只会冒泡了
- java算法 冒泡排序 与 快速排序思想
- C语言-用指针实现冒泡排序
- 5-30 字符串的冒泡排序
- 选择排序和冒泡排序
- 用回调函数实现冒泡排序
- 按多字段快速、冒泡排序实现 含CodeTimer性能测试
- C# 冒泡排序实现
- 排序算法(一)冒泡排序
- 排序算法之冒泡排序的思想以及Java实现
- 选择排序与冒泡排序
- 【排序】冒泡排序
- 排序:冒泡排序
- 排序——冒泡排序