源码系列:快速排序
来源:互联网 发布:上海java不好找转什么 编辑:程序博客网 时间:2024/04/26 14:25
quick_sort.cpp
#include <iostream>#include <vector>#include <iterator>#include <ctime>#include <algorithm>using namespace std;namespace algo{ /// 随机采样快排 平均效率O(nlgn)且常数因子很小 最坏效率O(n^2) void QuickSort(vector<int> &toSort,int beginIndex,int endIndex) { if(beginIndex < endIndex) { int random_swap = (rand() % (endIndex - beginIndex + 1)) + beginIndex; swap(toSort[random_swap],toSort[endIndex]); int i = beginIndex; for(int j=beginIndex;j!=endIndex;j++) { if(toSort[j] < toSort[endIndex]) { swap(toSort[i],toSort[j]); i++; } } swap(toSort[i],toSort[endIndex]); QuickSort(toSort,beginIndex,i-1); QuickSort(toSort,i+1,endIndex); } }}测试代码:
#include <iostream>#include <vector>#include <ctime>#include <iterator>#include "quick_sort.cpp"using namespace std;int main(){ cout << "快速排序" << endl; vector<int> toSort; for(int i=0;i<100;i++) { toSort.push_back(rand()); } cout << "随机填充100个数:" << endl; copy(toSort.begin(),toSort.end(),ostream_iterator<int>(cout,"\t")); algo::QuickSort(toSort,0,toSort.size()-1); cout << endl << "快排结果:" << endl; copy(toSort.begin(),toSort.end(),ostream_iterator<int>(cout,"\t")); return 0;}
测试结果:
0 0
- 源码系列:快速排序
- 排序系列--快速排序
- 算法系列-快速排序
- 快速排序算法源码
- 快速排序算法源码
- 快速排序源码
- 快速排序源码
- 快速排序算法源码
- QuickSort快速排序源码
- 快速排序(源码)
- 排序系列之快速排序
- 排序系列之快速排序
- 排序系列之快速排序
- 排序系列之快速排序
- 排序算法系列:快速排序
- 数据结构算法之排序系列Java、C源码实现(6)--快速排序
- 算法学习系列-快速排序
- 【算法系列】快速排序算法
- C语言的一些算法
- 关于时区AS3
- Ural 1313. Some Words about Sport
- 常见android面试题及答案
- 23种设计模式
- 源码系列:快速排序
- JS常用代码块
- 解决安装ubuntu系统后无法回到windows系统的问题
- 一步一步学Spring.Net——2、第一个程序“Hello,World”
- java swing的一些知识点
- activity完美退出
- 数字转Excel列标
- 设计模式之旅单一职责原则
- source insight快捷键及使用技巧