#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. SIGPIPE了解

    当未连接成功相机网络,在socket请求中 if (_session->prepareSession("192.168.1.1") != ICH_SUCCEED)    // ...

  2. j.APR连接器整体框图(含SSL实现分析)

    APR连接器的思路和bio,nio的整体架构也是类似的,可以看到下面的整体框图: 第一个区别是,对于从Acceptor线程中的socket解析这块,无论是nio还是bio都是在Acceptor线程内直 ...

  3. Python学习笔记之条件、循环和其他语句

    一.函数导入 1.为模块提供别名 >>> import math as foobar #设置math 别名为foobar >>> foobar.sqrt(4) 显示 ...

  4. 使用scanner工具类来获取用户输入的信息

    使用scanner工具类来获取用户输入的成绩信息. 步骤:1.导入java.util.Scanner包 2.创建Scanner对象 3.接受并保存用户输入的值 例子:通过用户输入来获取学生成绩 pac ...

  5. json转实体类

    VS快速生成JSON数据格式对应的实体 Json生成类在线生成工具 http://tool.sufeinet.com/Creater/JsonClassGenerator.aspx http://js ...

  6. tab切换-淘宝案例

    案例: html: <body> <div class="wrap" id="wrap"> <div class="no ...

  7. 以forin的方式遍历数组时进行删除操作的注意点

    今天在修改某项需求的时候,需要在遍历的时候将匹配项移除掉,采用的时forin的方式遍历,然后运行的时候却crash掉了 for (NSString*str in self.btnArray) { if ...

  8. Linux安装脚本需要交互之如何实现自动安装

    Linux中shell脚本运行时经常需要进行交互,比如安装软件的过程中对license声明的确认,需要输入yes,回车之类的确认信息.这个在自动化安装的时候就会是个问题. 通常对于这个问题比较灵活的解 ...

  9. (SenchaTouch+PhoneGap)开发笔记(2)开发环境搭建二

    一.Java环境和Android SDK  1.安装JDK和JRE JRE会在JDK安装完成后自动出现安装界面. 安装完成后,设置环境变量 JAVA_HOME    D:\Program Files\ ...

  10. IOS常用框架

    IOS开发中有用的第三方库 #Objective-C中最受瞩目库 [链接](https://github.com/languages​​/Objective-C/most_watched) * [th ...