c++的排序算法

一:merge 是 C++ STL 中的一个算法函数,用于将两个已排序的序列合并成一个有序序列。

template<class InputIterator1, class InputIterator2, class OutputIterator, class Compare> OutputIterator merge(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2, OutputIterator result, Compare comp);

功能:

将两个已排序的范围 [first1, last1)[first2, last2) 合并到范围 [result, result + (last1 - first1) + (last2 - first2)) 中,使用比较器 comp 进行元素的比较。

返回值:

返回指向存储结果的范围的尾后迭代器 result + (last1 - first1) + (last2 - first2)

cpp 复制代码
//merge算法,容器元素合并,存储到另一个容器
// merge 函数只能将两个有序的范围合并成一个有序的范围,而不能自动对合并后的范围进行排序

void test01()
{
	vector<int>v1;
	vector<int>v2;
	/*for (int i = 0; i < 5; i++)
	{
		v1.push_back(i + 1);
		v2.push_back(i + 2);
	}*/
	for (int i = 5; i >=0; i--)
	{
		v1.push_back(i + 1);
		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());
	//如果数据是降序,那么第六个参数写great<int>()
	merge(v1.begin(), v1.end(), v2.begin(), v2.end(), v3.begin(),greater<int>());
	for_each(v3.begin(), v3.end(), [](int val)->void {cout << val << " "; });
	
}

二:sort 是 C++ STL 中的一个算法函数,用于对指定范围内的元素进行排序。

函数原型:

复制代码

cpp复制代码

template<class RandomAccessIterator> void sort(RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class Compare> void sort(RandomAccessIterator first, RandomAccessIterator last, Compare comp);

功能:

对范围 [first, last) 内的元素进行排序。

返回值:

无。

cpp 复制代码
//sort算法
struct mysort
{
	bool operator()(int v1, int v2)
	{
		return v1 > v2;
	}
};
void test02()
{
	vector<int>v = { 2,35,634,523 };
	sort(v.begin(), v.end(),mysort());
	for_each(v.begin(), v.end(), [](int val)->void {cout << val << " "; });
}

random_shuffle 是 C++ STL 中的一个算法函数,用于对指定范围内的元素进行乱序排列。

template<class RandomAccessIterator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last); template<class RandomAccessIterator, class RandomNumberGenerator> void random_shuffle(RandomAccessIterator first, RandomAccessIterator last, RandomNumberGenerator&& gen);

功能:

对范围 [first, last) 内的元素进行随机乱序。

返回值:

无。

cpp 复制代码
//random_shuffle 乱序
void test03()
{
	srand((unsigned int)time(NULL));
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}
	for_each(v.begin(), v.end(), [](int val)->void {cout << val << " "; });
	cout << endl;
	random_shuffle(v.begin(), v.end());
	for_each(v.begin(), v.end(), [](int val)->void {cout << val << " "; });
}

reverse 是 C++ STL 中的一个算法函数,用于反转指定范围内的元素顺序。

template<class BidirectionalIterator> void reverse(BidirectionalIterator first, BidirectionalIterator last);

功能:

对范围 [first, last) 内的元素进行反转。

返回值:

无。

cpp 复制代码
//reverse 反转指定范围元素
void test04()
{
	vector<int>v;
	for (int i = 0; i < 10; i++)
	{
		v.push_back(i + 1);
	}
	for_each(v.begin(), v.end(), [](int val)->void {cout << val << " "; });
	cout << endl;
	reverse(v.begin(), v.end());
	for_each(v.begin(), v.end(), [](int val)->void {cout << val << " "; });
}
相关推荐
rainFFrain1 小时前
Boost搜索引擎项目(详细思路版)
网络·c++·http·搜索引擎
long_run1 小时前
C++之模板函数
c++
NuyoahC1 小时前
笔试——Day43
c++·算法·笔试
秋难降2 小时前
别再用暴力排序了!大小顶堆让「取极值」效率飙升至 O (log n)
python·算法·排序算法
彷徨而立2 小时前
【C++】 using声明 与 using指示
开发语言·c++
一只鲲3 小时前
48 C++ STL模板库17-容器9-关联容器-映射(map)多重映射(multimap)
开发语言·c++
智践行3 小时前
C++11 智能指针:`std::unique_ptr`、`std::shared_ptr`和`std::weak_ptr`
c++
智践行4 小时前
C++11之后的 Lambda 表达式 以及 `std::function`和`std::bind`
c++
智践行4 小时前
C++11移动语义‘偷梁换柱’实战
c++
祁同伟.4 小时前
【C++】模版(初阶)
c++