Detailed explanation of sort() function

Preface

As the name implies, sort() is a sorting function. It uses different sorting methods according to the specific situation, which is more efficient.
Generally speaking, it is not recommended to use the qsort function in the C language, because qsort is more cumbersome to use and involves a lot of pointer operations.
In addition, sort avoids the extreme situations that may occur in classic quick sort that may cause the actual complexity to degenerate to O(n 2 ) in its implementation.
Not much to say, let's take a look at the use of the sort() function.

Use sort

Required header files

#include<algorithm>

Other things needed:

using namespace std;

The method used is as follows:

sort(首元素地址(必填), 尾元素地址的下一个地址(必填), 比较函数(非必填));

The comparison function can be filled in according to its own needs. If the comparison function is not written, the interval given above will be sorted in ascending order by default.

Example: sort int and double arrays to sort
Insert picture description here
char arrays:
Insert picture description here

其实你会发现一个规律。
例如: 你要排前三个数  sort(a,a+3);
你要排前六个数 sort(a,a+6);
你要排前n个数  sort(a,a+n);

We should know that if the sequence needs to be sorted, then the elements in the sequence must be comparable, so it is necessary to formulate sorting rules to establish this comparability.
Especially like the structure, there is no relationship between the size and the comparison rules.
The third optional parameter of sort is the compare function (generally written as cmp function), which is used to implement this rule.

Implement the comparison function cmp

If you want to sort from big to small, you have to use the comparison function cmp to "tell" sort when to swap elements (reverse the comparison of the size of the elements).
Insert picture description here
Insert picture description here
Insert picture description here

Sorting of structure array

Now the following structure is defined:

struct student
{
    
    
	int x,y;
};

If you want to sort the structure array according to x from large to small, you can write the cmp function like this

bool cmp(student a,student b)
{
    
    
	return a.x>b.x;//按x值从大到小对结构体排序 
}

When the structure array is sorted, the cmp function must be written to specify the rules.
An example is as follows:
Insert picture description here
if you want to sort from largest to smallest by x (ie, first-level sort), but when x is equal, sort from smallest to largest by the size of y (ie, second-level sort).
Then the wording of cmp is:

bool cmp(student a,student b)
{
    
    
	if(a.x==b.x)//x值相等时按y从小到大排 
	{
    
    
		return a.y<b.y;
	} 
	return a.x>b.x;//按x值从大到小对结构体排序 
}bool cmp(student a,student b)
{
    
    
	if(a.x != b.x)//x值不相等时按x从大到小排 
    return a.x>b.x;
	else 
	return a.y<b.y;//x相等时按y值从小到大对结构体排序 
}

Insert picture description here

Sorting of containers

In the STL standard container, only vector, string, and deque can use sort.
This is because containers such as set and map are implemented with red-black trees (just understand), and the elements themselves are ordered, so sort sorting is not allowed.
Example: vector:
Insert picture description here
Example: string:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/qq_46527915/article/details/114597901