STL中的排序算法详解

1. 所有STL sort算法函数的名字列表:

函数名             功能描述

sort          对给定区间所有元素进行排序

stable_sort     对给定区间所有元素进行稳定排序

partial_sort     对给定区间所有元素部分排序

partial_sort_copy    对给定区间复制并排序

nth_element     找出给定区间的某个位置对应的元素

is_sorted               判断一个区间是否已经排好序

partition         使得符合某个条件的元素放在前面

stable_partition      相对稳定的使得符合某个条件的元素放在前面

 

2. 比较函数:

当你需要按照某种特定方式进行排序时,你需要给sort指定比较函数,否则程序会自动提供给你一个比较函数

vector < int > vect;

sort(vect.begin(), vect.end());//此时相当于调用

sort(vect.begin(), vect.end(), less<int>() );

sort 中的其他比较函数

equal_to 相等

not_equal_to 不相等

less 小于

greater 大于

less_equal 小于等于

greater_equal 大于等于

上述例子中系统 自己为sort提供了less仿函数。在STL中还提供了其他仿函 数,以下是仿函数列表: 不能直接写入仿 函数的名字,而是要写其重载的()函数: less<int>();

当你的容器中元 素时一些标准类型(int float char)或者string时,你可以直 接使用这些函数模板。但如果你时自己定义的类型或者你需要按照其他方式排序,你可以有两种方法来达到效果:一种是自己写比较函数另一种是重载类型的'<'操作赋

 

3. 全排序:

全排序即把所给定范围所有的元素按照大小关系顺序排列。sort采用的是成熟的"快速排序算法"(目前大部分STL版本已经不是采用简单的快速排序,而是结合内插排序算法)。复杂度为n*log(n)。stable_sort采用的是"归并排序",分派足够内存时,其算法复杂度为n*log(n), 否则 其复杂度为n*log(n)*log(n),其优点是会保持相等元素之间的相对位置在排序前后保持一致。

用于全排序的函 数有:

1.void sort(RandomAccessIterator first, RandomAccessIterator last);

2.void sort(RandomAccessIterator first, RandomAccessIterator last,StrictWeakOrdering comp);

3.void stable_sort(RandomAccessIterator first, RandomAccessIterator last);

4.void stable_sort(RandomAccessIterator first, RandomAccessIterator last, StrictWeakOrdering comp);

 

实例: 

#include <vector>
#include <algorithm> // Include algorithms
#include <iostream>
#include <string>

using namespace std;

typedef struct
{
	string first;
	string last;
}NAME;

bool sortbyfirst(const NAME& n1, const NAME& n2)
{
	return (n1.first<n2.first);
}

bool sortbylast(const NAME& n1, const NAME& n2)
{
	return (n1.last<n2.last);
}

int main(int argc, char* argv[])
{
	vector<NAME> contacts;
	vector<NAME>::iterator j;

	NAME tmp;
	tmp.first = "liu";
	tmp.last = "bei";
	contacts.push_back(tmp);

	tmp.first = "zhao";
	tmp.last = "yun";
	contacts.push_back(tmp);

	tmp.first = "gun";
	tmp.last = "yu";
	contacts.push_back(tmp);

	tmp.first = "zhang";
	tmp.last = "fei";
	contacts.push_back(tmp);

	cout<<"by first:"<<endl; 

	sort(contacts.begin(), contacts.end(), sortbyfirst);

	for(j=contacts.begin(); j!= contacts.end(); j++)
	{
		cout<<j->first<<" "<<j->last<<endl;
	}

	cout<<endl<<"by last:"<<endl;

	sort(contacts.begin(), contacts.end(), sortbylast);

	for(j=contacts.begin(); j!= contacts.end(); j++)
	{
		cout<<j->first<<" "<<j->last<<endl;
	}
	return 0;
}

/*
输出:

by first:
gun yu
liu bei
zhang fei
zhao yun

by last:
liu bei
zhang fei
gun yu
zhao yun
*/


 

/* stable_sort: 功能和sort是一样的,唯一不同的是,元素间的原有相对位置在排序前后没有变化。
				(带有"stable"字样的排序,元素间的原有相对位置在排序前后没有变化。)

				stable_sort的用法与sort一致,区别是stable_sort函数遇到两个数相等时,不对其交换顺序;
				这个应用在数组里面不受影响,当函数参数传入的是结构体时,会发现两者之间的明显区别。

template <class RandomAccessIterator>
  void stable_sort ( RandomAccessIterator first, RandomAccessIterator last );

template <class RandomAccessIterator, class Compare>
  void stable_sort ( RandomAccessIterator first, RandomAccessIterator last,
                     Compare comp );
*/

#include <iostream>     // std::cout
#include <algorithm>    // std::stable_sort
#include <vector>       // std::vector

bool compare_as_ints (double i,double j)
{
  return (int(i)<int(j));
}

int main () {
  double mydoubles[] = {3.14, 1.41, 2.72, 4.67, 1.73, 1.32, 1.62, 2.58};

  std::vector<double> myvector;

  myvector.assign(mydoubles,mydoubles+8);

  std::cout << "using default comparison:";
  std::stable_sort (myvector.begin(), myvector.end());
  for (std::vector<double>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  myvector.assign(mydoubles,mydoubles+8);

  std::cout << "using 'compare_as_ints' :";
  std::stable_sort (myvector.begin(), myvector.end(), compare_as_ints);
  for (std::vector<double>::iterator it=myvector.begin(); it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  system("pause");
  return 0;
}

/*
Output:

using default comparison: 1.32 1.41 1.62 1.73 2.58 2.72 3.14 4.67
using 'compare_as_ints' : 1.41 1.73 1.32 1.62 2.72 2.58 3.14 4.67
请按任意键继续. . .
*/


 

4. 局部排序:

partial_sort采用的堆排序(heapsort),它在任何情况下的复杂度都是n*log(n)。

局部排序其实是为了减少不必要的操作而提供的排序方式。

其函数原型为:

4.1 void partial_sort(RandomAccessIterator first, RandomAccessIterator middle,RandomAccessIterator last);

4.2 void partial_sort(RandomAccessIterator first,RandomAccessIterator middle,RandomAccessIterator last, StrictWeakOrdering comp);

4.3 RandomAccessIterator partial_sort_copy(InputIterator first, InputIteratorlast,RandomAccessIteratorresult_first,RandomAccessIterator result_last);

4.4 RandomAccessIterator partial_sort_copy(InputIterator first, InputIteratorlast,RandomAccessIteratorresult_first,RandomAccessIterator result_last, Compare comp);

例如:班上有1000个学生,我想知道分数最低的5名是哪些人。

partial_sort(vect.begin(),vect.begin()+5,vect.end(),less<student>());

 

实例: 

 

/*
template <class RandomAccessIterator>
  void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
                     RandomAccessIterator last);
 
custom (2) template <class RandomAccessIterator, class Compare>
  void partial_sort (RandomAccessIterator first, RandomAccessIterator middle,
                     RandomAccessIterator last, Compare comp); 

对迭代器first和last所指定范围中的元素进行偏序排序.经过排序的元素放到序列的前面部分,其余元素放到迭代器middle和last指定的范围内。

例如:
有一个赛跑成绩的集合,我们想知道前三名的成绩但并不关心其他名次的次序,可以这样对这个序列进行排序。
partial_sort(scores.begin(),scores.begin()+3,scores.end());
*/

/*
template <class InputIterator, class RandomAccessIterator>
  RandomAccessIterator
    partial_sort_copy (InputIterator first,InputIterator last,
                       RandomAccessIterator result_first,
                       RandomAccessIterator result_last);
 
custom (2) template <class InputIterator, class RandomAccessIterator, class Compare>
  RandomAccessIterator
    partial_sort_copy (InputIterator first,InputIterator last,
                       RandomAccessIterator result_first,
                       RandomAccessIterator result_last, Compare comp); 
*/

#include <iostream>     // std::cout
#include <algorithm>    // std::partial_sort
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i<j); }

int main () {
  int myints[] = {9,8,7,6,5,4,3,2,1};
  std::vector<int> myvector (myints, myints+9);

  // using default comparison (operator <):
  std::partial_sort (myvector.begin(), myvector.begin()+5, myvector.end());

  // using function as comp
  std::partial_sort (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);

  // 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';


  std::vector<int> myvector2(5);
  std::partial_sort_copy(myvector.begin(), myvector.begin()+5, myvector2.begin(), myvector2.end(), myfunction);
  // print out content:
  std::cout << "myvector2 contains:";
  for (std::vector<int>::iterator it=myvector2.begin(); it!=myvector2.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  system("pause");
  return 0;
}

/*
Output:

myvector contains: 1 2 3 4 5 9 8 7 6
myvector2 contains: 1 2 3 4 5
请按任意键继续. . .
*/


5. nth_element 指定元素排序

5.1 void nth_element(RandomAccessIterator first, RandomAccessIterator nth, RandomAccessIterator last);

5.2 void nth_element(RandomAccessIterator first, RandomAccessIterator nth,RandomAccessIterator last,

StrictWeakOrdering comp);

例如:班上有1000个学生,我想知道分数排在倒数第4名的学生。

nth_element(vect.begin(), vect.begin()+3, vect.end(),less<student>());

 

实例: 

 

/* nth_element: 作用为求第n小的元素,并把它放在第n位置上,下标是从0开始计数的,也就是说求第0小的元素就是最小的数。
				nth_element不会对整体进行排序。

   需要注意的是,此算法只对“随机访问迭代器”有效(如 vector),如果需要对 list 使用此算法,可先将 list 的所有元素
   拷贝至 vector(或者存储 list::iterator,对自定义类型效率更高),再使用此算法。
*/

/*
template <class RandomAccessIterator>
  void nth_element (RandomAccessIterator first, RandomAccessIterator nth,
                    RandomAccessIterator last);
 
custom (2) template <class RandomAccessIterator, class Compare>
  void nth_element (RandomAccessIterator first, RandomAccessIterator nth,
                    RandomAccessIterator last, Compare comp); 
*/

#include <iostream>     // std::cout
#include <algorithm>    // std::nth_element, std::random_shuffle
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i<j); }

int main () {
  std::vector<int> myvector;

  // set some values:
  for (int i=1; i<10; i++) myvector.push_back(i);   // 1 2 3 4 5 6 7 8 9

  std::random_shuffle (myvector.begin(), myvector.end());

  // using default comparison (operator <):
  std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end());

  // using function as comp
  std::nth_element (myvector.begin(), myvector.begin()+5, myvector.end(),myfunction);

  // 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 0;
}

/*
Output:

myvector contains: 1 2 3 4 5 6 7 8 9
请按任意键继续. . .
*/


6. partition 和stable_partition

partition就是把一个区间中的元素按照某个条件分成两类,并没有排序。

其函数原型为:

6.1 ForwardIterator partition(ForwardIterator first, ForwardIterator last, Predicate pred)

6.2 ForwardIterator stable_partition(ForwardIterator first, ForwardIterator last, Predicate pred);

例如:班上10个学生,计算所有没有及格(低于60分)的学生:

student exam("pass", 60);

stable_partition(vect.begin(), vect.end(), bind2nd(less<student>(), exam));

 

实例: 

 

/* partition: 将序列按指定条件来划分成2部分(第一部分是符合条件的所有元素),返回第二部分的第一个元素的位置。
			  注意,序列被划分成2部分后,各个元素的相对位置往往已被打乱。

template <class ForwardIterator, class UnaryPredicate>
  ForwardIterator partition (ForwardIterator first,
                             ForwardIterator last, UnaryPredicate pred);
*/

#include <iostream>     // std::cout
#include <algorithm>    // std::partition
#include <vector>       // std::vector

bool IsOdd (int i) { return (i%2)==1; }

int main () {
  std::vector<int> myvector;

  // set some values:
  for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

  std::vector<int>::iterator bound;
  bound = std::partition (myvector.begin(), myvector.end(), IsOdd);

  // print out content:
  std::cout << "odd elements:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  std::cout << "even elements:";
  for (std::vector<int>::iterator it=bound; it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  system("pause");
  return 0;
}

/*
Output:

odd elements: 1 9 3 7 5
even elements: 6 4 8 2
请按任意键继续. . .
*/


/* stable_partition: 功能和partition是一样的,唯一不同的是,序列被划分成2部分后,各个元素原有的相对位置没有变化。
					 (带有"stable"字样的排序,元素间的原有相对位置在排序前后没有变化。)

template <class BidirectionalIterator, class UnaryPredicate>
  BidirectionalIterator stable_partition (BidirectionalIterator first,
                                          BidirectionalIterator last,
                                          UnaryPredicate pred);
*/

#include <iostream>     // std::cout
#include <algorithm>    // std::stable_partition
#include <vector>       // std::vector

bool IsOdd (int i) { return (i%2)==1; }

int main () {
  std::vector<int> myvector;

  // set some values:
  for (int i=1; i<10; ++i) myvector.push_back(i); // 1 2 3 4 5 6 7 8 9

  std::vector<int>::iterator bound;
  bound = std::stable_partition (myvector.begin(), myvector.end(), IsOdd);

  // print out content:
  std::cout << "odd elements:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=bound; ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  std::cout << "even elements:";
  for (std::vector<int>::iterator it=bound; it!=myvector.end(); ++it)
    std::cout << ' ' << *it;
  std::cout << '\n';

  system("pause");
  return 0;
}

/*
Output:

odd elements: 1 3 5 7 9
even elements: 2 4 6 8
请按任意键继续. . .
*/


 

7. 效率由高到低:

partion

stable_partition

nth_element

partial_sort

sort

stable_sort

 

8.如何选择恰当的排序算法:

8.1 若需对vector, string, deque, 或array容器进行全排序,你可选择sort或stable_sort;

8.2 若只需对vector, string, deque, 或array容器中取得top n的元素,部分排序partial_sort是首选.

8.3 若对于vector, string, deque, 或array容器,你需要找到第n个位置的元素或者你需要得到top n且不关系top n中的内部 顺序,nth_element是最 理想的;

8.4 若你需要从标准序列容器或者array中把满足某个条件 或者不满足某个条件的元素分开,你最好使用partition或stable_partition;

8.5 若使用的list容器,你可以直接使用partition和stable_partition算法,你可以使用list::sort代替sort和stable_sort排 序。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值