The usage of sort() function in C++ and how to sort the class according to a certain member parameter

.Foreword
When coding, the ordering of elements is a basic and important content. We have learned many sorting algorithms, but should we re-encode every time we use it?

In fact, C++ has already encapsulated a very convenient algorithm sort() for us, we only need to introduce the header file <algorithm> to use it.

.Syntax description The
sort() function requires three parameters:, sort(begin,end,cmp)where:
begin is the pointer to the first element of
the array to be sorted, end is the pointer to the next position of the last element of the array to be sorted,
cmp is OK Choose parameters as our ranking criteria. In the absence of the third parameter, the ascending order is defaulted, but the sorting method can be changed by defining cmp.

.No third parameter example

#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

int main()
{
    
    
	int a[6] = {
    
     5,2,6,1,8,2 };
	vector<int> v(a,a+6);
	sort(a, a + 6);//或sort(std::begin(a), std::end(a));
	for(int i=0;i<6;i++)
	{
    
    
		cout << a[i] << endl; //输出1 2 2 5 6 8
	}
	sort(v.begin(), v.end());
	for(int i=0;i<6;i++)
	{
    
    
		cout << v[i] << endl; //输出1 2 2 5 6 8  
	}
	return 0;
}

.Define the third parameter

#include <iostream>
#include <algorithm>
using namespace std;

bool cmp(int x,int y)
{
    
    
	return x > y;//第一参数大于第二参数表示降序
}
int main()
{
    
    
	int a[] = {
    
     8,2,6,5,1,2 };
	sort(std::begin(a), std::end(a), cmp);
	for(int i=0;i<6;i++)
	{
    
    
		cout << a[i] << endl;//结果为8 6 5 2 2 1
	}
	return 0;
}

.Why is it recommended to use sort()? The
sort function is more efficient in most cases.
The following excerpt: https://blog.csdn.net/qq_36378681/article/details/102816726 (very good article)

What sort is used to implement the sort function?
In fact, the sort in STL is a kind of mixed sort, which applies quick sort, heap sort, and insertion sort. The following is the situation when each sort is applied:

Quick sort is used at the beginning.
When the recursion depth exceeds logn, in order to prevent the degradation of quick sort , sort will be used instead of heap sort.
When the recursion depth is less than logn, but the interval length is less than or equal to 16, use insertion sort instead.

Why not use heap sort in the first place?
Why would the interviewer ask this? Because he has asked you about the implementation of heap sort and quick sort, at this time you are already very clear about their space-time complexity. At this time, you realize that the space complexity of heap sort is actually lower than that of quick sort (heap sort o (1), quick sort o(logn)), and heap sort will not degenerate.

So the interviewer wants to know why quick sort is more efficient than heap sort?

It’s really difficult to answer this question if you haven’t understood it

In fact, when we study algorithms, we study theoretical complexity. In actual engineering applications, we also consider the actual efficiency of the algorithm. This not only involves the theoretical complexity of the algorithm, but also involves issues such as hardware and operating systems.

And quick sort is a typical example

The same is the time complexity of nlogn, why the overall efficiency of heap sort and merge sort is not as good as quick sort? This is because there is a cache area in the computer hardware. Its access and read speed is very fast (much faster than the memory). It is usually integrated on the CPU, so its capacity is very limited. Usually the CPU will often The accessed data is temporarily stored in the cache, and the CPU will also find the data from the cache first.

Because quick sort uses a hub , this hub has a very large number of visits, then it will be put into the cache, then the efficiency of accessing the hub will be very high, so the overall efficiency of quick sort will be better than the other several sorts high.

How to use the sort() function to sort a class by a certain member parameter
As mentioned earlier, the sorting method of the sort function is realized by the third parameter, the function object. The specific operations are as follows.

Example:

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

//定义有复数参数的类
class item
{
    
    
public:
	int type_A;//类成员参数A
	int type_B;//类成员参数B

	item(int a, int b) :type_A(a), type_B(b) {
    
    }
};

//以下排序默认降序
//按照参数A排序
bool cmpA(const item& x,const item& y)
{
    
    
	return x.type_A < y.type_A;
}
//按照参数B排序
bool cmpB(const item& x, const item& y)
{
    
    
	return x.type_B < y.type_B;
}
int main()
{
    
    
	vector<item> arr;
	item t1(1, 3), t2(5, 2), t3(3, 1);
	arr.push_back(t1);
	arr.push_back(t2);
	arr.push_back(t3);
	cout << "未排序前:" << endl;
	for (int i = 0;i < 3;i++)
	{
    
    
		cout << "<x,y>= " << arr[i].type_A << " " << arr[i].type_B << endl;
	}
	//按照参数A排序
	sort(arr.begin(), arr.end(), cmpA);
	cout << "按照参数A排序:" << endl;
	for(int i=0;i<3;i++)
	{
    
    
		cout << "<x,y>= " << arr[i].type_A << " " << arr[i].type_B<<endl;
	}
	//按照参数B排序
	sort(arr.begin(), arr.end(), cmpB);
	cout << "按照参数B排序:" << endl;
	for (int i = 0;i < 3;i++)
	{
    
    
		cout << "<x,y>= " << arr[i].type_A << " " << arr[i].type_B<<endl;
	}
	return 0;
}

result:

未排序前:
<x,y>= 1 3
<x,y>= 5 2
<x,y>= 3 1
按照参数A排序:
<x,y>= 1 3
<x,y>= 3 1
<x,y>= 5 2
按照参数B排序:
<x,y>= 3 1
<x,y>= 5 2
<x,y>= 1 3

Guess you like

Origin blog.csdn.net/qq_43530773/article/details/114502100