今天学习常用排序算法。
学习目标:
- 掌握常用的排序算法
算法简介:
sort
//对容器内元素进行排序random_shuffle
//洗牌 指定范围内的元素随机调整次序merge
// 容器元素合并,并存储到另一容器中reverse
// 反转指定范围的元素
1 sort
功能描述:
- 对容器内元素进行排序
函数原型:
-
sort(iterator beg, iterator end, _Pred);
// 按值查找元素,找到返回指定位置迭代器,找不到返回结束迭代器位置
// beg 开始迭代器
// end 结束迭代器
// _Pred 谓词
#include<iostream>
using namespace std;
#include<string>
#include <vector>
#include <algorithm>
void myPrint(int val)
{
cout << val << " ";
}
int main()
{
vector<int>v;
v.push_back(1);
v.push_back(6);
v.push_back(3);
v.push_back(7);
v.push_back(4);
v.push_back(8);
//sort默认从小到大排序
sort(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
//从大到小排序,使用内建函数对象
sort(v.begin(), v.end(),greater<int>());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
system("pause");
return 0;
}
**总结:**sort属于开发中最常用的算法之一,需熟练掌握
2 random_shuffle
功能描述:
- 洗牌 指定范围内的元素随机调整次序
函数原型:
-
random_shuffle(iterator beg, iterator end);
// 指定范围内的元素随机调整次序
// beg 开始迭代器
// end 结束迭代器
#include<iostream>
using namespace std;
#include<string>
#include <vector>
#include <algorithm>
#include<ctime>
void myPrint(int val)
{
cout << val << " ";
}
void test01()
{
srand((unsigned int)time(NULL));
vector<int>v;
for (int i = 0; i < 10; i++)
{
v.push_back(i);
}
for_each(v.begin(), v.end(), myPrint);
cout << endl;
random_shuffle(v.begin(), v.end());
for_each(v.begin(), v.end(), myPrint);
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
**总结:**random_shuffle洗牌算法比较实用,使用时记得加随机数种子
3 merge
功能描述:
- 两个容器元素合并,并存储到另一容器中
函数原型:
-
merge(iterator beg1, iterator end1, iterator beg2, iterator end2, iterator dest);
// 容器元素合并,并存储到另一容器中
// 注意: 两个容器必须是有序的
// beg1 容器1开始迭代器
// end1 容器1结束迭代器
// beg2 容器2开始迭代器
// end2 容器2结束迭代器
// dest 目标容器开始迭代器
#include<iostream>
using namespace std;
#include<string>
#include <vector>
#include <algorithm>
#include<functional>
#include<ctime>
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
srand((unsigned int)time(NULL));
vector<int>v1;
vector<int>v2;
for (int i = 0; i < 10; i++)
{
v1.push_back(i);
v2.push_back(i+2);
}
vector<int>v3;
v3.resize(v1.size() + v2.size());
merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin());
for_each(v3.begin(), v3.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
**总结:**merge合并的两个容器必须是有序序列,同升序或者同降序。
4 reverse
功能描述:
- 将容器内元素进行反转
函数原型:
-
reverse(iterator beg, iterator end);
// 反转指定范围的元素
// beg 开始迭代器
// end 结束迭代器
#include<iostream>
using namespace std;
#include<string>
#include <vector>
#include <algorithm>
#include<functional>
#include<ctime>
class MyPrint
{
public:
void operator()(int val)
{
cout << val << " ";
}
};
void test01()
{
srand((unsigned int)time(NULL));
vector<int>v;
v.push_back(3);
v.push_back(6);
v.push_back(2);
v.push_back(8);
cout << "反转前:" << endl;
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
reverse(v.begin(), v.end());
for_each(v.begin(), v.end(), MyPrint());
cout << endl;
}
int main()
{
test01();
system("pause");
return 0;
}
*总结:**reverse反转区间内元素,面试题可能涉及到
学习学习学习学nm
。
打个小广告,欢迎关注我的公众号“麦香E站”,分享人工智能,信通学习,数学提高,外语学习,生活理财等多方面知识以及我多年积累的学习资源分享~
公众号后台回复 “编程” 获取我多年来积攒的海量编程电子书~
后台回复“latex”获取我倾力所作latex快速入门手册~