快速排序的实现
来源:互联网 发布:手机设计图纸软件 编辑:程序博客网 时间:2024/06/06 07:03
前后对比
原
#include <iostream>#include <vector>#include <cstdbool>using namespace std;bool SwitchInt (int& a, int &b){ if (a == b) return true; a += b; b = a - b; a -= b; return true;}bool QuickSort (vector<int> &ivec, vector<int>::iterator small, vector<int>::iterator big){ if (small == big) return true; vector<int>::iterator start = small; vector<int>::iterator end = big; vector<int>::iterator flag = small; while (small != big){ if (*small > *flag){ SwitchInt(*small, *flag); flag = small; small++; continue; } if (*big < *flag){ SwitchInt(*big, *flag); flag = big; big--; continue; } } QuickSort(ivec, start, small); QuickSort(ivec, big, end); return true;}int main (void){ vector<int> ivec; int num = 0; while (cin >> num){ ivec.push_back(num);} QuickSort(ivec, ivec.begin(), ivec.end()); for (int num: ivec) cout << num; return 0;}
现
//// bool SwitchInt (int& a, int &b){// if (a == b) return true;// a += b;// b = a - b;// a -= b;// return true;// }// 标准SWAP方法, 比上面的更快#include <vector>#include <iostream>#include <ctime>#include <cstdlib>#include <algorithm>using namespace std;void SwitchInt (int& a, int &b){ int t =a; a=b; b=t;}// vector<int> &ivec 参数无用,去掉// bool 返回值无用,去掉// big 使用标准库end语法/*bool*/ void QuickSort (/*vector<int> &ivec,*/ vector<int>::iterator small, vector<int>::iterator big){ if (small == big) return /*true*/; vector<int>::iterator start = small; vector<int>::iterator end = big; vector<int>::iterator flag = small; //随机选取flag, 将小于等于此值的放在左边,大于此值的放于右边 for (small++; small != big; small++){ if (*small <= *flag){ SwitchInt(*small, *flag); //此时small的值为flag,需要前移的较小值已经移动完毕,flag指向其 flag++; SwitchInt(*small, *flag); //把位于samll位置的flag换回来,并将最前面一个大于flag的值换到small上 // flag = small; // small++; // continue; } // if (*big < *flag){ // SwitchInt(*big, *flag); // flag = big; // big--; // continue; // } } cout << &*start << ' ' << &*flag << ' ' << '\n'; //QuickSort(/*ivec,*/ start, small); QuickSort(/*ivec,*/ start, flag); // QuickSort(/*ivec,*/ big, end); QuickSort(/*ivec,*/ ++flag, end); return /*true*/;}int main(void){ srand(time(0)); vector<int> ivec; int num = 0; // while (cin >> num){ // ivec.push_back(num);} for (int n=0; n<10; n++) { cout << (num = rand()) << " "; ivec.push_back(num); } cout << "\n"; QuickSort(/*ivec,*/ ivec.begin(), ivec.end()); for_each(ivec.begin(), ivec.end(), [](int num){ cout << num << " ";}); system("pause"); return 0;}
总结
我以前使用vector和数组的时候可能有一个奇怪的习惯,不知道其他人有没有:函数调用数组/vector时会把这个数组/vector传入进去。这样虽然可以获得更为完整的信息,但是并没有这个必要,反而会降低程序的安全性。以前我在有闲心的时候会在函数定义的时候加上const来防止发生一些奇怪的事情,然而并没有这个必要,其实只需要把数组/vector的头和尾元素传入进去就好了,在这个程序里就显得尤为明显。
我有的时候在思考一些问题的时候确实有些思维僵化了,如上面网友给出的修改建议:交换需要交换的元素后flag后移,然后再交换flag和small。虽然会改变一趟快排中较大元素的排列,但是并不影响该算法的结果。而之前我的想法有些混乱,由课本上的算法(数据结构)和我自己的想法混合而成,然而并没有能够很好的结合起来,导致原QuickSort中的逻辑混乱。这可能需要较多的练习才能改正过来
0 0
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- 快速排序的实现
- asp.net core mvc权限控制:在视图中控制操作权限
- hdu1034(模拟题)
- 1041. Be Unique 解析
- 宝钗
- 2017-2-10-w3cschool编程挑战 -javascript
- 快速排序的实现
- hibernate中两表一对一关系 和 session.save()方法和 session.saveOrUpdate()方法
- 1042. Shuffling Machine 解析
- PAT 1045.快速排序
- sessionStorage 、localStorage 和 cookie 对比区分
- DP问题 导弹拦截
- VC++ 跳过MFC框架 处理键盘设备消息
- 关于PCB走线
- django入门与学习-django的模板文件-2