STL (first bullet)-sort

STL (first bullet)-sort function

Today we will talk about the sort (fast sort) function in STL

Its time complexity is O (nlogn) O(nlogn)O ( n l o g n )

Why does log exist? Because it uses dichotomy and random numbers (the most important thing, no random number will degenerate into O (n 2) O(n^2)O ( n2)

The sort function is divided into two types:

  1. sort (_RAIter, _RAIter)
  2. sort (_RAIter, _RAIter, _Compare)

After translation, it is sort (array name + subscript of the number to start sorting (inclusive), array name + (subscript of the number to stop sorting +1))

Normally, sort always sorts integer arrays in ascending order. The
code is as follows:

#include<cstdio>
#include<algorithm>
using namespace std;
int x[1010],n;
int main()
{
    
    
	scanf("%d",&n);//输入需要排序的数的个数
	for(int i=1;i<=n;i++)
		scanf("%d",&x[i]);//输入
	sort(x+1,x+1+n);//升序排序
	for(int i=1;i<=n;i++)
		printf("%d ",x[i]);//输出
} 

The results of the program are as follows Insert picture description here
. How can we sort in descending order? A comparison function is needed. (Personally like to define the comparison function as cmp (abbreviation for compare))

code show as below:

#include<cstdio>
#include<algorithm>
using namespace std;
int x[1000],n;
bool cmp(int a,int b)
{
    
    
	return a>b;//若此处为return a<b的话则是升序排序
}
int main()
{
    
    
	scanf("%d",&n);//输入需要排序的数的个数
	for(int i=1;i<=n;i++)
		scanf("%d",&x[i]);//输入
	sort(x+1,x+1+n,cmp);//降序排序
	for(int i=1;i<=n;i++)
		printf("%d ",x[i]);//输出
} 

There is something special when sort judges whether to change the numbers in the two arrays:

If the cmp function returns true, do not change, return false, then change (it may be a bit hard to think about here)

The personal identification method is any one a = xi, b = xj (i <j) a=x_i,b=x_j(i<j)a=xi,b=xj(i<j ) will match the above relational expression a>b in the x array aftersorting. The
results are as follows:
Insert picture description here
There are also some structures that can also be sorted. If you have learned the structure, you can think about it + specific operations, its name Sort keywords(this is what our teacher calls, if you are unprofessional, don’t spray it) _{(our teacher calls it like this, don’t spray if you are unprofessional)}( I have old teacher this kind of call is , not exclusively industry 's words do not spray )

Here is a classic question, you can take a look together

Topic:
In order to allow teachers to better rank mid-term exam scores, please help teachers use a program to quickly rank the first to last names

  1. Those with the highest overall score are ranked first
  2. If the total score is the same, the one with the highest Chinese score will be ranked first
  3. If the Chinese score is the same, the one with the highest math score will be ranked first
  4. If the math scores are the same, those with the highest English scores will be ranked first
  5. If the English scores are the same, the student number is ranked first (when the teacher enters the student score, the student will always enter the student number from low to high)

code show as below:

#include<cstdio>
#include<algorithm>
using namespace std;
int n;
struct cj{
    
    
	int yuwen,shuxue,yingyu,total;
	int xuehao;
	char name[100];
}x[1010];
bool cmp(cj a,cj b)
{
    
    
	if(a.total!=b.total)return a.total>b.total;//第一关键字
	if(a.yuwen!=b.yuwen)return a.yuwen>b.yuwen;//第二关键字
	if(a.shuxue!=b.shuxue)return a.shuxue>b.shuxue;//第三关键字
	if(a.yingyu!=b.yingyu)return a.yingyu>b.yingyu;//第四关键字
	return a.xuehao<b.xuehao;//第五关键字
}
int main()
{
    
    
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
    
    
		scanf("%s%d%d%d",x[i].name,&x[i].yuwen,&x[i].shuxue,&x[i].yingyu);
		x[i].xuehao=i;
		x[i].total=x[i].yuwen+x[i].shuxue+x[i].yingyu;
	}
	sort(x+1,x+1+n,cmp);
	for(int i=1;i<=n;i++)
	{
    
    
		printf("%s\n",x[i].name);
	}
} 

The results are as follows:
Insert picture description here
here is the basic usage of all sorts, see you next time! ! !

Guess you like

Origin blog.csdn.net/Ricky2007/article/details/109428243