#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. Linux 内核编译

    注:该文章部分内容摘录自以下链接. http://www.cnblogs.com/zhunian/archive/2012/04/04/2431883.html 创建内核的第一步是创建一个 .conf ...

  2. 【IOS 开发】Object - C 入门 之 数据类型详解

    1. 数据类型简介及输出() http://www.把下面的替换我.com/html/topnews201408/79/1279.htm csdn123

  3. Webpack、Browserify和Gulp

    https://www.zhihu.com/question/37020798 https://www.zhihu.com/question/35479764

  4. zsh下 home end 键失效的解决办法

    我的环境是 centos 6.5 x64 安装 oh my zsh 后,home end 键失效,解决办法为在 .zshrc 里添加设置如下 #Rebind HOME and END to do th ...

  5. html的a链接的href怎样才另起一个页面

    在后面加上target ="_blank",就可以,正如: <ul class="nav navbar-nav navbar-right" style=& ...

  6. 4.AE中的缩放,书签

    1.书签 private void textBox1_TextChanged(object sender, EventArgs e) { if (txtBookmark.Text == "& ...

  7. ant学习简单例子

    1.下载ant,http://ant.apache.org/ 这个网站下载,然后配置环境变量 打开dos界面,输入ant -version,如果提示命令不存在,进入到ant包装目录bin下载,再次运行 ...

  8. 使用SecureCRT远程链接Ubuntu出现 Change of username or service not allowed的问题

    RT:    首先是确认ubuntu上有运行 sshd服务的 但是用SecureCRT链接时报错,默认用户名是root: 打开ssh_config检查下是否禁止直接用root登陆 这句 改成yes o ...

  9. DotnetBar在VS2010工具箱中不显示问题

    请参考:http://blog.csdn.net/yanbo710148546/article/details/7862819

  10. 尺寸不固定的图片在div中垂直居中并完全显示

    前几天做一个项目,需要批量上传图片,图片外侧div尺寸固定:由于图片是用户输入的,所以大小存在不确定性,产品需求是无论图片尺寸多大,都要垂直居中完全显示 废话不多说,直接上代码 html <ul ...