#include <iostream>
#include <string>
#include <vector>
using namespace std;
void QuickSort_danny();
void QSort(vector<int>& ivec, vector<int>::iterator low,vector<int>::iterator high);

void QuickSort_danny()
{
int input;
vector<int> ivec;
while(cin>>input)
{
ivec.push_back(input);
}
for (vector<int>::iterator iter=ivec.begin(); iter!=ivec.end(); iter++)
{
cout << *iter << " ";
}

QSort(ivec,ivec.begin(),ivec.end()-1);

cout <<endl;
for (vector<int>::iterator iter=ivec.begin(); iter!=ivec.end(); iter++)
{
cout << *iter << " ";
}
}

vector<int>::iterator Partition(vector<int>&, vector<int>::iterator low,vector<int>::iterator high)
{
vector<int>::value_type pivokey = *low;
while(low<high)
{
while(low < high && *high > pivokey ) high--; // 若此处为 *high >= pivokey; 则对于5 8 5,进行快速排序仍然为 5 8 5, 不能将
// 序列变为 5 5 8. 且最后high和low均为第一个元素,故QSort中iter-1会出现越界错误;
*low = *high;
while(low < high && *low <= pivokey) low++;
*high =*low;
}
*low = pivokey;
return low;
}

void QSort(vector<int>& ivec, vector<int>::iterator low,vector<int>::iterator high)
{
if (low < high)
{
vector<int>::iterator iter = Partition(ivec, low, high);
QSort(ivec, low, iter-1);
QSort(ivec, iter+1, high);
}
}

对vector<int>进行快速排序的更多相关文章

  1. vector< vector<int> >类似于二维数组

    vector< vector<int> > intVV; vector<int> intV; int i,j; ;i<;++i){ intV.clear(); ...

  2. C++ sort vector<vector<int> > or vector<MyClass> 容器的排序

    C++的STL中提供了很强大的排序函数sort,可以对任意数组,结构体及类进行排序,下面我们先来看最简单的数组排序.默认的升序排列,我们也可以在后面加上less或greater来告诉编译器我们想要的排 ...

  3. vector<int> v2 = 42; 为何非法

    C++ Primer 第四版,第十三章“复制控制” 习题13.2,为何vector<int> v2 = 42; 不能编译? 百度贴吧里的一位楼主给出了答案,本人认为正确,特此引用: 参考链 ...

  4. const vector<int> 和 vector<const int>问题讨论

    1.const vector <int> vec(10) —— 与const int a[10]是一回事,意思是vec只有10个元素,不能增加了,里面的元素也是不能变化的 vector&l ...

  5. vector 利用swap 函数进行内存的释放 vector<int>().swap

    首先,vector与deque不同,其内存占用空间只会增长,不会减小.比如你首先分配了10,000个字节,然后erase掉后面9,999个,则虽然有效元素只有一个,但是内存占用仍为10,000个.所有 ...

  6. 使用vector<vector<int>>实现的一个二维数组

    本文为大大维原创,最早于博客园发表,转载请注明出处!!! 1 #include<iostream> #include<vector> using namespace std; ...

  7. 对多维向量vector<vector<int> > vec进行操作

    直接写作vector<vector<int> > vec在VC++6.0下编译不过改做:    typedef std::vector<int> ROW;    s ...

  8. 2016.07.13-vector<vector<int>>应用2——Two Sum扩展

    收获: vector<vector<int> >res,不能直接用res[j].push_back(number),因为res[j]是空的,没有初始化 可以先定义 vector ...

  9. 2016.6.24——vector<vector<int>>【Binary Tree Level Order Traversal】

    Binary Tree Level Order Traversal 本题收获: 1.vector<vector<int>>的用法 vector<vector<int ...

随机推荐

  1. SQL Server Lock Escalation - 锁升级

    Articles Locking in Microsoft SQL Server (Part 12 – Lock Escalation) http://dba.stackexchange.com/qu ...

  2. Hadoop 集群搭建

    Hadoop 集群搭建 2016-09-24 杜亦舒 目标 在3台服务器上搭建 Hadoop2.7.3 集群,然后测试验证,要能够向 HDFS 上传文件,并成功运行 mapreduce 示例程序 搭建 ...

  3. Python_Day7_面向对象学习

    1.面向对象编程介绍 2.为什么要用面向对象进行开发? 3.面向对象的特性:封装.继承.多态 4.类.方法. 面向过程 VS 面向对象 编程范式 编程是程序员用特定的语法+数据结构+算法组成的代码来告 ...

  4. Android View 简析

    基于android 4.4上源码分析: setContentView流程: getwindow() ->setContentView() -> installDecor() -> a ...

  5. ArcGIS创建tpk切片缓存

    一. 背景知识 1. tpk是什么? 从地图或底图生成切片,并将切片进行打包从而创建单个压缩的 .tpk 文件.切片包(.tpk)是在地图或栅格数据集中能作为 Web 切片或 Web 高程图层发布的一 ...

  6. WebStorm和sublime上使用git连接github(转)

    WebStorm使用git连接github的方法: 用webstorm上传代码时,首先要先下载git,网址一搜就可以搜到,安装git,并且把ssh配置到github上.然后开始配置webstorm,打 ...

  7. VPN安装后报错:Reason442 & Error56

    VPN安装后一直报错,同样的32位安装包别人安装是正常,自己安装就不正常了,考虑到是自己电脑配置的问题. 经过一番努力,解决了问题,下面就本次解决过程做一个小小的总结. (1)确保VPN Servic ...

  8. OpenBSD内核之引导PBR

    OpenBSD引导的第二部PBR,也是活动分区的一个扇区的代码,由第一步的MBR加载到0x7C00处,manpage里详细的讲解了过程和大致实现 biosboot(8) (http://man.ope ...

  9. 教你9个提升 Wordpress 网站安全性的方法

    大约一个月前,这个部落格被黑客入侵(编按:Amit Agarwal 的网站).而其他托管于相同主机商的网站像是 ctrlq.org 和2hundredzeros.com 也深受其害,黑客成功从网路上拿 ...

  10. boost.asio与boost.log同时使用导致socket不能正常收发数据

    现象: 1. 没有使用boost.log前能正常收发数据 2.加入boost.log后async_connect没有回调 fix过程: 1. gdb调试发现程序block在pthread_timed_ ...