#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. java:经典消费生成者

    产品类: public class Info { private String title = "生产"; private String content = "生产罐头& ...

  2. [原创] Easy SysLite V1.2 (2016.5.29更新,新增加WIN10支持,一个程序适配所有系统减肥)

    [原创] Easy SysLite V1.2 (2016.5.29更新,新增加WIN10支持,一个程序适配所有系统减肥) nohacks 发表于 2016-5-29 17:12:51 https:// ...

  3. [教程] [承風雅傳HSU]用ES4封裝Win7---ES4 Win7封裝教程(未完待續)

    [教程] [承風雅傳HSU]用ES4封裝Win7---ES4 Win7封裝教程(未完待續) a10036it 发表于 2015-7-27 21:11:19 https://www.itsk.com/t ...

  4. selenium遇到readonly元素的输入

    方法:去掉该元素的readonly属性 使用js来去掉 ((JavaScriptExecutor ) driver).executeScript($("input#{放置元素的CLASS}[ ...

  5. Unity3D 中的3种坐标系

    Unity3D Script API : Camera 若干文章: 1.Screen VS Viewport What is the difference 2.Screen,Viewport有什麽區別 ...

  6. 删除win8的网络连接记录

    打开注册表,HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles,修改各个子项里的P ...

  7. 油猴 greasemonkey 背景音乐 火狐 chrome 背景音乐

    火狐,chrome背景音乐 http://www.w3school.com.cn/tags/tag_audio.asp js插入背景音乐,猴油脚本使用 var audio = document.cre ...

  8. Web cookie 详解

    总结:服务端客户端变量建议都是用 utf-8字符集, 前后传递的变量都建议使用url编码处理.php setcookie 保存到客户端的变量会自动url编码的, 所以客户端获取后需要decodeURI ...

  9. 搭建一个分布式MongoDB鉴权集群

    今天休假在家,测试并搭建了一个replica set shard MongoDB鉴权集群.replica set shard 鉴权集群中文资料比较少,本文是个人笔记,同时也希望对后来者有所帮助.本文仅 ...

  10. spark发行版笔记4Spark Streaming事务处理彻底掌握

    Spark Streaming事务处理彻底掌握 感谢DT大数据梦工厂支持提供以下内容,DT大数据梦工厂专注于Spark发行版定制. 内容概括: 1Exactly once 2 输出不重复 1 正如银行 ...