【C++】常用排序算法

0.前言

1.sort


cpp 复制代码
#include <iostream>
using namespace std;


// 常用排序算法 sort
#include<vector>
#include<algorithm>

//利用仿函数 打印输出
class myPrint
{
public:
	void operator()(int val)
	{
		cout << val << " ";
	}
};

//利用普通函数 打印输出
void myPrint2(const int val) // const 可加可不加 因为形参不能改变实参
{
	cout << val << " ";
}

void test01()
{
	vector<int>v;
	v.push_back(10);
	v.push_back(50);
	v.push_back(30);
	v.push_back(20);
	v.push_back(60);
	v.push_back(10);

	//默认从小到大 升序 排序
	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(), myPrint2);
	cout << endl;
}

int main()
{
	test01();
	cout << "------------------------" << endl;
	//test02();
	//cout << "------------------------" << endl << endl;
	//test03();

	//**************************************
	system("pause");
	return 0;
} 

2.random_shuffle


cpp 复制代码
#include <iostream>
using namespace std;


// 常用排序算法 random_shuffle(洗牌算法,打乱顺序)
#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();
	cout << "------------------------" << endl;
	//test02();
	//cout << "------------------------" << endl << endl;
	//test03();

	//**************************************
	system("pause");
	return 0;
} 

3.merge

cpp 复制代码
#include <iostream>
using namespace std;


// 常用排序算法 merge
#include<vector>
#include<algorithm>

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v1;
	vector<int>v2;

	for (int i = 0; i < 10; i++)
	{
		v1.push_back(i);
		v2.push_back(i + 1);
	}

	//目标容器
	vector<int>vTarget;
	//提前给目标容器分配空间
	vTarget.resize(v1.size() + v2.size());

	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), vTarget.begin());

	for_each(vTarget.begin(), vTarget.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	cout << "------------------------" << endl;
	//test02();
	//cout << "------------------------" << endl << endl;
	//test03();

	//**************************************
	system("pause");
	return 0;
} 

4.reverse


cpp 复制代码
#include <iostream>
using namespace std;


// 常用排序算法 reverse
//反转 首尾互换
#include<vector>
#include<algorithm>

void myPrint(int val)
{
	cout << val << " ";
}

void test01()
{
	vector<int>v;
	
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i);
	}

	cout << "反转前:" << endl;
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;

	reverse(v.begin(), v.end());
	cout << "反转后:" << endl;
	for_each(v.begin(), v.end(), myPrint);
	cout << endl;
}

int main()
{
	test01();
	cout << "------------------------" << endl;
	//test02();
	//cout << "------------------------" << endl << endl;
	//test03();

	//**************************************
	system("pause");
	return 0;
} 
相关推荐
muyouking1111 分钟前
0.深入探秘 Rust Web 框架 Axum
开发语言·前端·rust
勇敢牛牛_12 分钟前
【Rust基础】使用Rocket构建基于SSE的流式回复
开发语言·后端·rust
汇太浪30 分钟前
第十六届蓝桥杯大赛软件赛省赛 C++ 大学 B 组 部分题解
c++·蓝桥杯
斯普信专业组32 分钟前
从原理到实践:NFS复杂故障处理方法论
开发语言·nfs
WW_千谷山4_sch36 分钟前
MYOJ_11700(UVA10591)Happy Number(快乐数)(超快解法:图论思想解题)
c++·算法
郭涤生1 小时前
QML 信号与槽
c++·笔记·qt
钢铁男儿1 小时前
Python 文本和字节序列(处理文本文件)
开发语言·python
Ethon_王1 小时前
C++ STL deque容器详解
c++
梦の1 小时前
C++Cherno 学习笔记day20 [81]-[85] 可视化基准测试、单例模式、小字符串优化sso、跟踪内存分配、左值与右值
c++·笔记·学习
加点油。。。。2 小时前
Matlab绘图(三)——设置图例的位置
开发语言·matlab·绘图