排序算法

1.对所有元素排序

  • void sort( RandomAccessIterator beg, RandomAccessIterator end)//使用operator< 对区间[beg, end)内所有元素排序(采用快速排序)
  • void sort( RandomAccessIterator beg, RandomAccessiterator end, BinaryPredicate op)//使用op(elem1,elem2)排序准则
  • void stable_sort( RandomAccessIterator beg, RandomAccessIterator end)//保证相等元素的原本次序排序后不变。(采用归并排序)
  • void stabel_sort( RandomAccessIterator beg, RandomAccessiterator end, BinaryPredicate op)
  • 注:不可用与list,相应的list提供一个成员函数sort()

2.局部排序

  • void partial_sort( RandomAccessIterator beg, RandomAccessIterator sortEnd, RandomAccessIterator end)//以operator< 对区间[beg, end)内的元素进行排序,使区间[beg, sortEnd) 内的元素处于有序状态。(采用堆排序)
  • void partial_sort( RandomAccessIterator beg, RandomAccessIterator sortEnd, RandomAccessIterator end, BinaryPredicate op)

// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	deque<int> coll;
	INSERT_ELEMENTS(coll,3,7);
	INSERT_ELEMENTS(coll,2,6);
	INSERT_ELEMENTS(coll,1,5);
	PRINT_ELEMENTS(coll);

	partial_sort(coll.begin(),coll.begin()+5,coll.end());
	PRINT_ELEMENTS(coll);

	partial_sort(coll.begin(),coll.begin()+5,coll.end(),greater<int>());
	PRINT_ELEMENTS(coll);
	partial_sort(coll.begin(),coll.end(),coll.end());
	PRINT_ELEMENTS(coll);
	return 0;
}

  • RandomAccessIterator partial_sort_copy( InputIterator sourceBeg, InputIterator sourceEnd, RandomAccessIterator destBeg, RandomAccessIterator destEnd)//copy()和partial_sort()的组合。将源区间[sourceBeg, sourceEnd)内的元素先排序,再复制到目标区间[destBeg, destEnd)。([sourceBeg, sourceEnd)内的元素不变)。返回目标区间内"最后一个被复制元素"的下一位置。(第一个未覆盖元素)
  • RandomAccessIterator partial_sort_copy( InputIterator sourceBeg, InputIterator sourceEnd, RandomAccessIterator destBeg, RandomAccessIterator destEnd, BinaryPredicate op)
  • 注:"被排序(被复制)的元素数量"是源区间和目标区间两者所含元素数量的较小值。

// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	deque<int> coll1;
	vector<int> coll6(6);
	vector<int> coll30(30);
	INSERT_ELEMENTS(coll1,3,7);
	INSERT_ELEMENTS(coll1,2,6);
	INSERT_ELEMENTS(coll1,1,5);
	PRINT_ELEMENTS(coll1);
	vector<int>::iterator pos6;
	pos6=partial_sort_copy(coll1.begin(),coll1.end(),coll6.begin(),coll6.end());
	copy(coll6.begin(),pos6,ostream_iterator<int>(cout," "));
	cout<<endl;
	vector<int>::iterator pos30;
	pos30=partial_sort_copy(coll1.begin(),coll1.end(),coll30.begin(),coll30.end(),greater<int>());
	copy(coll30.begin(),pos30,ostream_iterator<int>(cout," "));
	cout<<endl;
	return 0;
}

3.根据第n个元素排序

  • void nth_element( RandomAccessIterator beg, RandomAccessIterator nth, RandomAccessIterator end) 对区间[beg, end)内的元素进行排序,使第n个元素上的元素就位。[beg, nth)上的元素统统小于[nth, end)上的元素。即将nth将[beg, end)分成2部分,第一部分所有都"小于"第二部分的任何元素。
  • void nth_element( RandomAccessIterator beg, RandomAccessIterator nth, RandomAccessIterator end, BinaryPredicate op)
  • 注:如果需要n个最大或最小元素,但不要要求它们已序,那么这个算法就很有用。

4.Heap

  • void make_heap( RandomAccessIterator beg, RandomAccessIterator end, BinaryPredicate op= less) //将某区间内的元素转换成heap
  • void push_heap( RandomAccessIterator beg, RandomAccessIterator end, BinaryPredicate op= less) //将end之前的最后一个元素加入原本就是个heap的[beg, end-1)区间内,使整个区间[beg, end)成为一个heap
  • void pop_heap( RandomAccessIterator beg, RandomAccessIterator end, BinaryPredicate op= less) //将heap[beg, end)内的最高元素(即第一个元素)移到最后位置,并将剩余区间[beg, end-1)内的元素组织起来,成为一个新的heap
  • void sort_heap(RandomAccessIterator beg, RandomAccessIterator end, BinaryPredicate op= less) //将heap转换为一个已序群集(此后它就不在是个heap)

// STL.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <print.hpp>
using namespace std;

int _tmain(int argc, _TCHAR* argv[])
{
	vector<int> coll;
	INSERT_ELEMENTS(coll,3,7);
	INSERT_ELEMENTS(coll,5,9);
	INSERT_ELEMENTS(coll,1,4);
	PRINT_ELEMENTS(coll,"on entry: ");
	make_heap(coll.begin(),coll.end());
	PRINT_ELEMENTS(coll,"after make_heap: ");
	pop_heap(coll.begin(),coll.end());
	coll.pop_back();
	PRINT_ELEMENTS(coll,"after pop_heap: ");
	coll.push_back(17);
	push_heap(coll.begin(),coll.end());
	PRINT_ELEMENTS(coll,"after push_heap: ");
	sort_heap(coll.begin(),coll.end());
	PRINT_ELEMENTS(coll,"after sort_heap: ");
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值