排序算法总结std::sort

本文介绍了C++中的std::sort排序算法,它适用于随机存取的容器,如vector。std::sort可以对vector<int>进行升序排序,也可以按类或结构体的特定属性进行排序。示例中展示了如何对vector<Elem>按property1降序排序,以及std::sort的时间复杂度和工作原理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

std::sort不支持list,set,只支持随机存取的容器,如vector

std::sort排序的使用:

1,对vector<int>m_Vect升序排序,可以直接std::sort(m_Vect.begin(), m_Vect.end());

2,对vector<Elem>m_Vect排序,其中Elem是一个类,或者一个结构体,现在对vector排序是指按Elem中的某个属性排序。

如:

class Elem

{

private:

                     int property1;

                     int property2;

        public:

                 int getProperty1();

                 void set Property1();

                 int getProperty2();

                 void set Property2();               

};

 

typedef std::vector<Elem> ElemVect;

ElemVect m_ElemVect;

现在对m_ElemVect按照属性property1排序。

 

//降序排序

temple<typename T1, typename T2>

bool     DescendingOrder(T1  t1,  T2  t2)

{

return  t1.getProperty1() >   t2.getProperty1();

}

使用的地方:

std::sort(m_ElemVect.begin(),  m_ElemVect.end(),  DescendingOrder<Elem,  Elem>);

 

 

________________________________________________________________________________________________________________________

 

sort
Syntax:
  #include <algorithm>
  void sort( iterator start, iterator end );
  void sort( iterator start, iterator end, StrictWeakOrdering cmp );

 

The sort() algorithm sorts the elements in the range [start,end) into ascending order. If two elements are equal, there is no guarantee what order they will be in.

If the strict weak ordering function object cmp is given, then it will be used to compare two objects instead of the < operator.

The algorithm behind sort() is the introsort algorithm. sort() runs in O(N log(N)) time (average and worst case) which is faster than polynomial time but slower thanlinear time.

Example code:

For example, the following code sorts a vector of integers into ascending order:

 vector<int> v;
 v.push_back( 23 );
 v.push_back( -1 );
 v.push_back( 9999 );
 v.push_back( 0 );
 v.push_back( 4 );              

 cout << "Before sorting: ";
 for( unsigned int i = 0; i < v.size(); i++ ) {
   cout << v[i] << " ";
 }
 cout << endl;            

 sort( v.begin(), v.end() );            

 cout << "After sorting: ";
 for( unsigned int i = 0; i < v.size(); i++ ) {
   cout << v[i] << " ";
 }
 cout << endl;            

When run, the above code displays this output:

 Before sorting: 23 -1 9999 0 4
 After sorting: -1 0 4 23 9999          

Alternatively, the following code uses the sort() function to sort a normal array of integers, and displays the same output as the previous example:

 int array[] = { 23, -1, 9999, 0, 4 };
 unsigned int array_size = 5;           

 cout << "Before sorting: ";
 for( unsigned int i = 0; i < array_size; i++ ) {
   cout << array[i] << " ";
 }
 cout << endl;            

 sort( array, array + array_size );             

 cout << "After sorting: ";
 for( unsigned int i = 0; i < array_size; i++ ) {
   cout << array[i] << " ";
 }
 cout << endl;            

This next example shows how to use sort() with a user-specified comparison function. The functioncmp is defined to do the opposite of the < operator. When sort() is called withcmp used as the comparison function, the result is a list sorted in descending, rather than ascending, order:

 bool cmp( int a, int b ) {
   return a > b;
 }              

 ...            

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

 cout << "Before: ";
 for( int i = 0; i < 10; i++ ) {
   cout << v[i] << " ";
 }
 cout << endl;            

 sort( v.begin(), v.end(), cmp );               

 cout << "After: ";
 for( int i = 0; i < 10; i++ ) {
   cout << v[i] << " ";
 }
 cout << endl;      

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值