◆ 常用的排序算法:

1.1、合并(容器A(全部/部分)&容器B(全部/部分)==>容器C(全部/部分),容器C中元素已经排好顺序),返回的值==>iteratorOutBegin.end()

iterator merge(iterator1Begin, iterator1And, iterator2Begin, iterator2End, iteratorOutBegin);

iterator merge(iterator1Begin, iterator1And, iterator2Begin, iterator2End, iteratorOutBegin, functor排序); // ZC: 这里的functor指定,排序的话采用何种方式

1.2、排序

void sort(iteratorBegin, iteratorEnd);

void sort(iteratorBegin, iteratorEnd, functor排序);

1.3、随机洗牌(shuffle是洗牌的意思)(记得初始化随机种子)

void random_shuffle(iteratorBeign, iteratorEnd);

// ZC: 下面的functor作用是,通过容器中某个元素(传入参数) 计算得到它在容器中的新的位置,然后与这个位置上的原来的元素进行位置交换

void random_shuffle(iteratorBeign, iteratorEnd, functor产生随机位置);

1.4、反转

void reverse(iteratorBegin, iteratorEnd);

1、

◆ 以下是排序和通用算法:提供元素排序策略

1.1、第6讲 PPT.25

◆ merge() :    合并两个有序序列,存放到另一个序列。
ZC: 最终得到的result容器里面的元素是经过有序的
ZC: 有两种参数格式,返回值是 iterator[ 该值==result容器.end() ]

ZC: VC6 测试代码 - 1:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; void main()
{
vector<int> vecIntA;
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back();
vecIntA.push_back(); vector<int> vecIntB;
vecIntB.push_back();
vecIntB.push_back();
vecIntB.push_back();
vecIntB.push_back(); vector<int> vecIntC;
vecIntC.resize(); // 扩大容量 // ZC: 如果不做这一步,VC6编译的exe就崩溃了
merge(
vecIntA.begin(), vecIntA.end(),
vecIntB.begin(), vecIntB.end(),
vecIntC.begin());
vector<int>::iterator it = vecIntC.begin();
// ZC: 从这里打印的信息来看,它自动做了排序处理
int iIdx = ;
while (it != vecIntC.end())
{
printf("[%02d] ==> *it : %d\n", iIdx, *it);
it ++;
iIdx ++;
}
}

ZC:控制台输出 - 1:

 [00] ==> *it : 1
[01] ==> *it : 2
[02] ==> *it : 3
[03] ==> *it : 4
[04] ==> *it : 5
[05] ==> *it : 6
[06] ==> *it : 7
[07] ==> *it : 8
[08] ==> *it : 9
Press any key to continue

ZC: VC6 测试代码 - 2:

来自msdn的例子(https://msdn.microsoft.com/en-us/library/9ew9xdb2(v=vs.80).aspx)

 // alg_merge.cpp
// compile with: /EHsc
#include <vector>
#include <algorithm>
#include <functional> // For greater<int>( )
#include <iostream> // Return whether modulus of elem1 is less than modulus of elem2
bool mod_lesser ( int elem1, int elem2 )
{
if (elem1 < )
elem1 = - elem1;
if (elem2 < )
elem2 = - elem2;
return elem1 < elem2;
} int main()
{
using namespace std;
vector <int> v1a, v1b, v1 ( );
vector <int>::iterator Iter1a, Iter1b, Iter1; // Constructing vector v1a and v1b with default less than ordering
int i;
for ( i = ; i <= ; i++ )
v1a.push_back( i ); int ii;
for ( ii =- ; ii <= ; ii++ )
v1b.push_back( ii ); cout << "Original vector v1a with range sorted by the\n "
<< "binary predicate less than is v1a = ( " ;
for ( Iter1a = v1a.begin( ) ; Iter1a != v1a.end( ) ; Iter1a++ )
cout << *Iter1a << " ";
cout << ")." << endl; cout << "Original vector v1b with range sorted by the\n "
<< "binary predicate less than is v1b = ( " ;
for ( Iter1b = v1b.begin ( ) ; Iter1b != v1b.end ( ) ; Iter1b++ )
cout << *Iter1b << " ";
cout << ")." << endl; // Constructing vector v2 with ranges sorted by greater
vector <int> v2a ( v1a ) , v2b ( v1b ) , v2 ( v1 );
vector <int>::iterator Iter2a, Iter2b, Iter2;
sort ( v2a.begin ( ) , v2a.end ( ) , greater<int> ( ) );
sort ( v2b.begin ( ) , v2b.end ( ) , greater<int> ( ) ); cout << "Original vector v2a with range sorted by the\n "
<< "binary predicate greater is v2a = ( " ;
for ( Iter2a = v2a.begin ( ) ; Iter2a != v2a.end ( ) ; Iter2a++ )
cout << *Iter2a << " ";
cout << ")." << endl; cout << "Original vector v2b with range sorted by the\n "
<< "binary predicate greater is v2b = ( " ;
for ( Iter2b = v2b.begin ( ) ; Iter2b != v2b.end ( ) ; Iter2b++ )
cout << *Iter2b << " ";
cout << ")." << endl; // Constructing vector v3 with ranges sorted by mod_lesser
vector <int> v3a ( v1a ), v3b ( v1b ) , v3 ( v1 );
vector <int>::iterator Iter3a, Iter3b, Iter3;
sort ( v3a.begin ( ) , v3a.end ( ) , mod_lesser );
sort ( v3b.begin ( ) , v3b.end ( ) , mod_lesser ); cout << "Original vector v3a with range sorted by the\n "
<< "binary predicate mod_lesser is v3a = ( " ;
for ( Iter3a = v3a.begin ( ) ; Iter3a != v3a.end ( ) ; Iter3a++ )
cout << *Iter3a << " ";
cout << ")." << endl; cout << "Original vector v3b with range sorted by the\n "
<< "binary predicate mod_lesser is v3b = ( " ;
for ( Iter3b = v3b.begin ( ) ; Iter3b != v3b.end ( ) ; Iter3b++ )
cout << *Iter3b << " ";
cout << ")." << endl; // To merge inplace in ascending order with default binary
// predicate less <int> ( )
merge ( v1a.begin ( ) , v1a.end ( ) , v1b.begin ( ) , v1b.end ( ) , v1.begin ( ) );
cout << "Merged inplace with default order,\n vector v1mod = ( " ;
for ( Iter1 = v1.begin( ) ; Iter1 != v1.end( ) ; Iter1++ )
cout << *Iter1 << " ";
cout << ")." << endl; // To merge inplace in descending order, specify binary
// predicate greater<int>( )
merge ( v2a.begin ( ) , v2a.end ( ) , v2b.begin ( ) , v2b.end ( ) ,
v2.begin ( ) , greater <int> ( ) );
cout << "Merged inplace with binary predicate greater specified,\n "
<< "vector v2mod = ( " ;
for ( Iter2 = v2.begin( ) ; Iter2 != v2.end( ) ; Iter2++ )
cout << *Iter2 << " ";
cout << ")." << endl; // Applying A user-defined (UD) binary predicate mod_lesser
vector <int>::iterator itRtn = merge ( v3a.begin ( ) , v3a.end ( ) , v3b.begin ( ) , v3b.end ( ) ,
v3.begin ( ) , mod_lesser );
cout << "Merged inplace with binary predicate mod_lesser specified,\n "
<< "vector v3mod = ( " ; ;
for ( Iter3 = v3.begin( ) ; Iter3 != v3.end( ) ; Iter3++ )
cout << *Iter3 << " ";
cout << ")." << endl; itRtn --;
itRtn --;
cout << "*itRtn : " << *itRtn;
}

ZC:控制台输出 - 2 (vs2010):

 Original vector v1a with range sorted by the
binary predicate less than is v1a = ( 0 1 2 3 4 5 ).
Original vector v1b with range sorted by the
binary predicate less than is v1b = ( -5 -4 -3 -2 -1 0 ).
Original vector v2a with range sorted by the
binary predicate greater is v2a = ( 5 4 3 2 1 0 ).
Original vector v2b with range sorted by the
binary predicate greater is v2b = ( 0 -1 -2 -3 -4 -5 ).
Original vector v3a with range sorted by the
binary predicate mod_lesser is v3a = ( 0 1 2 3 4 5 ).
Original vector v3b with range sorted by the
binary predicate mod_lesser is v3b = ( 0 -1 -2 -3 -4 -5 ).
Merged inplace with default order,
vector v1mod = ( -5 -4 -3 -2 -1 0 0 1 2 3 4 5 ).
Merged inplace with binary predicate greater specified,
vector v2mod = ( 5 4 3 2 1 0 0 -1 -2 -3 -4 -5 ).
Merged inplace with binary predicate mod_lesser specified,
vector v3mod = ( 0 0 1 -1 2 -2 3 -3 4 -4 5 -5 ).
*itRtn : 5
请按任意键继续. . .

1.2、第6讲 PPT.26

◆ sort() :  以默认升序的方式重新排列指定范围内的元素。若要改排序规则,可以输入比较函数。

ZC: 有两种参数格式,返回值是 void 。

ZC: VC6 测试代码 - 1:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; void main()
{
vector<int> vecInt;
vecInt.push_back();
vecInt.push_back();
vecInt.push_back();
vecInt.push_back();
vecInt.push_back(); sort(vecInt.begin(), vecInt.end()); vector<int>::iterator it = vecInt.begin();
// ZC: 从这里打印的信息来看,它自动做了排序处理
int iIdx = ;
while (it != vecInt.end())
{
printf("[%02d] ==> *it : %d\n", iIdx, *it);
it ++;
iIdx ++;
}
}

ZC:控制台输出 - 1:

 [00] ==> *it : 1
[01] ==> *it : 2
[02] ==> *it : 3
[03] ==> *it : 4
[04] ==> *it : 5
[05] ==> *it : 6
[06] ==> *it : 7
[07] ==> *it : 8
[08] ==> *it : 9
Press any key to continue

ZC: VC6 测试代码 - 2:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; //学生类
class CStudent
{
public:
CStudent(int iID, string strName)
{
m_iID=iID;
m_strName=strName;
}
public:
int m_iID;
string m_strName;
}; //学号比较函数
bool Compare(const CStudent &stuA, const CStudent &stuB)
{
return (stuA.m_iID < stuB.m_iID);
} void main()
{
vector<CStudent> vecStu;
vecStu.push_back(CStudent(, "老二"));
vecStu.push_back(CStudent(, "老大"));
vecStu.push_back(CStudent(, "老三"));
vecStu.push_back(CStudent(, "老四")); sort(vecStu.begin(), vecStu.end(), Compare);
// 此时,vecStu容器包含了按顺序的"老大对象","老二对象","老三对象","老四对象" vector<CStudent>::iterator it = vecStu.begin();
while (it != vecStu.end())
{
printf("%s\n", it->m_strName.c_str());
it ++;
}
}

ZC:控制台输出 - 2:

 老大
老二
老三
老四
Press any key to continue

1.3、第6讲 PPT.30

◆ random_shuffle() :     对指定范围内的元素随机调整次序。

ZC: 有两种参数格式,返回值是 void

ZC: VC6 测试代码 - 1:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <time.h> #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; void main()
{
//设置随机种子
srand(time()); vector<int> vecInt;
vecInt.push_back();
vecInt.push_back();
vecInt.push_back();
vecInt.push_back();
vecInt.push_back(); string str("UIPower"); random_shuffle(vecInt.begin(), vecInt.end()); //随机排序,结果比如:9,7,1,5,3
random_shuffle(str.begin(), str.end()); //随机排序,结果比如:"owreUIP" vector<int>::iterator it = vecInt.begin();
int iIdx = ;
while (it != vecInt.end())
{
printf("[%02d] ==> %d\n", iIdx, *it);
it ++;
} printf("str : %s\n", str.c_str());
}

ZC:控制台输出 - 1:

 [00] ==> 9
[00] ==> 1
[00] ==> 3
[00] ==> 5
[00] ==> 7
str : IroePUw
Press any key to continue

ZC: VC6 测试代码 - 2(来自:http://www.cplusplus.com/reference/algorithm/random_shuffle/):

 // random_shuffle example
#include <iostream> // std::cout
#include <algorithm> // std::random_shuffle
#include <vector> // std::vector
#include <ctime> // std::time
#include <cstdlib> // std::rand, std::srand // random generator function:
int myrandom (int i)
{
int iRand = std::rand();
int iRtn = iRand % i;
printf("%d %% %d ==> %d\n", iRand, i, iRtn);
return iRtn; //return std::rand()%i;
} int main () {
std::srand ( unsigned ( std::time() ) );
std::vector<int> myvector; // set some values:
for (int i=; i<; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9 // using built-in random generator: // ZC: built-in ==> 内置
//std::random_shuffle ( myvector.begin(), myvector.end() ); // ZC: 先把这句代码注释掉,暂时只关注自定义的随机生成函数 // using myrandom:
std::random_shuffle ( myvector.begin(), myvector.end(), myrandom); printf("\n"); // print out content:
std::cout << "myvector contains:";
for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
std::cout << ' ' << *it; std::cout << '\n'; system("pause");
return ;
}

ZC:控制台输出 - 2:

 4094 % 2 ==> 0
3749 % 3 ==> 2
18714 % 4 ==> 2
6449 % 5 ==> 4
634 % 6 ==> 4
29214 % 7 ==> 3
14374 % 8 ==> 6
8421 % 9 ==> 6 myvector contains: 2 1 4 7 6 5 9 3 8
请按任意键继续. . .

ZC:我理解的自定义随机函数变换方式:

1 2 3 4 5 6 7 8 9   原来的顺序(对于第0个元素 不做随机运算)
2 1 3 4 5 6 7 8 9 第1个元素(数字"2") 和 第0个元素 交换位置之后的结果
2 1 3 4 5 6 7 8 9 第2个元素(数字"3") 和 第2个元素 交换位置之后的结果(位置都没动)
2 1 4 3 5 6 7 8 9 第3个元素(数字"4") 和 第2个元素 交换位置之后的结果
2 1 4 3 5 6 7 8 9 第4个元素(数字"5") 和 第4个元素 交换位置之后的结果(位置都没动)
2 1 4 3 6 5 7 8 9 第5个元素(数字"6") 和 第4个元素 交换位置之后的结果
2 1 4 7 6 5 3 8 9 第6个元素(数字"7") 和 第3个元素 交换位置之后的结果
2 1 4 7 6 5 8 3 9 第7个元素(数字"8") 和 第6个元素 交换位置之后的结果
2 1 4 7 6 5 9 3 8 第8个元素(数字"9") 和 第6个元素 交换位置之后的结果

1.4、第6讲 PPT.30

◆ reverse() :   对指定范围内元素重新反序排序。

ZC: 只有一种参数格式,返回值是 void

ZC: VC6 测试代码:

 #ifdef WIN32
#pragma warning (disable: 4786)
#endif #include <string>
#include <vector>
#include <set> #include <algorithm> // 算法
#include <numeric> // 算法
#include <functional> // 算法 using namespace std; void main()
{
vector<int> vecInt;
vecInt.push_back();
vecInt.push_back();
vecInt.push_back();
vecInt.push_back();
vecInt.push_back(); reverse(vecInt.begin(), vecInt.end()); //{9,7,5,3,1} vector<int>::iterator it = vecInt.begin();
int iIdx = ;
while (it != vecInt.end())
{
printf("[%02d] ==> %d\n", iIdx, *it);
it ++;
}
}

ZC:控制台输出:

 [00] ==> 9
[00] ==> 7
[00] ==> 5
[00] ==> 3
[00] ==> 1
Press any key to continue

?.?、第6讲 PPT.?

ZC: VC6 测试代码:

ZC:控制台输出:

C

STL_算法_02_排序算法的更多相关文章

  1. javascript数据结构与算法--高级排序算法

    javascript数据结构与算法--高级排序算法 高级排序算法是处理大型数据集的最高效排序算法,它是处理的数据集可以达到上百万个元素,而不仅仅是几百个或者几千个.现在我们来学习下2种高级排序算法-- ...

  2. 在Object-C中学习数据结构与算法之排序算法

    笔者在学习数据结构与算法时,尝试着将排序算法以动画的形式呈现出来更加方便理解记忆,本文配合Demo 在Object-C中学习数据结构与算法之排序算法阅读更佳. 目录 选择排序 冒泡排序 插入排序 快速 ...

  3. c/c++ 通用的(泛型)算法 之 只读算法,写算法,排序算法

    通用的(泛型)算法 之 只读算法,写算法,排序算法 只读算法: 函数名 功能描述 accumulate 求容器里元素的和 equal 比较2个容器里的元素 写算法 函数名 功能描述 fill 用给定值 ...

  4. javascript数据结构与算法--高级排序算法(快速排序法,希尔排序法)

    javascript数据结构与算法--高级排序算法(快速排序法,希尔排序法) 一.快速排序算法 /* * 这个函数首先检查数组的长度是否为0.如果是,那么这个数组就不需要任何排序,函数直接返回. * ...

  5. javascript数据结构与算法--基本排序算法(冒泡、选择、排序)及效率比较

    javascript数据结构与算法--基本排序算法(冒泡.选择.排序)及效率比较 一.数组测试平台. javascript数据结构与算法--基本排序(封装基本数组的操作),封装常规数组操作的函数,比如 ...

  6. JS中算法之排序算法

    1.基本排序算法 1.1.冒泡排序 它是最慢的排序算法之一. 1.不断比较相邻的两个元素,如果前一个比后一个大,则交换位置. 2.当比较完第一轮的时候最后一个元素应该是最大的一个. 3.按照步骤一的方 ...

  7. STL源代码分析——STL算法sort排序算法

    前言 因为在前文的<STL算法剖析>中,源代码剖析许多,不方便学习,也不方便以后复习.这里把这些算法进行归类,对他们单独的源代码剖析进行解说.本文介绍的STL算法中的sort排序算法,SG ...

  8. stl_algorithm算法之排序算法

    排序算法: 注意:容器中必须重载 op< ,排序中stl标准中要求用小于来进行比较. 7.53.sort //全排序. 7.54.stable_sort //稳定排序.两个或两个以上的相邻且相等 ...

  9. 面试常用算法总结——排序算法(java版)

    排序算法 重要性不言而喻,很多算法问题往往选择一个好的排序算法往往问题可以迎刃而解 1.冒泡算法 冒泡排序(Bubble Sort)也是一种简单直观的排序算法.它重复地走访过要排序的数列,一次比较两个 ...

随机推荐

  1. nodejs 将网上的图片下载到本地文件

    var request = require('request'); var fs = require('fs'); var img_src = 'https://www.baidu.com/img/b ...

  2. cc150 --链表中倒数第k个节点

    题目描述 输入一个链表,输出该链表中倒数第k个结点.   快指针先走K步,然后快慢同时走,快走到末尾时,慢指针就是倒数第个.     public class Solution { public Li ...

  3. Understanding Convolutional Neural Networks for NLP

    When we hear about Convolutional Neural Network (CNNs), we typically think of Computer Vision. CNNs ...

  4. 消息系统之Apache ActiveMQ

    一.下载运行MQ服务 1.下载ActiveMQ :http://activemq.apache.org/ 2.解压缩: 进入bin目录 win32和win64对应不同位的操作系统,选择进入 点击act ...

  5. linux make命令安装详解

    对于GNU Make或许很多Windows开发的程序员并不是很了解,因为Windows中的很多集成开发环境(IDE)都帮我们做了这件事.但是作为一个专业从事 Linux嵌入式开发的程序员就必须要了解G ...

  6. 很全的linux网络编程技巧

    本文转载自:http://www.cnblogs.com/jfyl1573/p/6476607.html 1. LINUX网络编程基础知识 1 1.1. TCP/IP协议概述 1 1.2. OSI参考 ...

  7. C++原创应用类库和工具类库

    此博文记载着自编C++应用类库和生成器库的源代码的链接地址,并且对库的开发环境.开发过程.缺陷以及改进更新进行说明. 分数类 利用中午的时间,自己在Visual Studio 2013环境下编写了一个 ...

  8. IEEE发布2017年编程语言排行榜:Python高居首位,java第三,php第八

    2017年7月18日,IEEE Spectrum 发布了第四届顶级编程语言交互排行榜.因为有各种不同语言的排行,所以 IEEE Spectrum 依据不同的变量对流行度进行了排行.据 IEEE Spe ...

  9. svn忽略目录,svn忽略app目录add toignore list,避免每次更新很多app的内容下来导出到本地很麻烦

    svn忽略目录,svn忽略app目录add toignore list,避免每次更新很多app的内容下来导出到本地很麻烦 ------------------------------ 本人微信公众帐号 ...

  10. python之路----进程二

    守护进程 会随着主进程的结束而结束. 主进程创建守护进程 其一:守护进程会在主进程代码执行结束后就终止 其二:守护进程内无法再开启子进程,否则抛出异常:AssertionError: daemonic ...