算法笔记--sort函数,openjudge的6377:生日相同 2.0

一、sort函数基本概念
1、Sort()函数是c++一种排序方法之一,类似于快速排序,时间复杂度为n*log2(n)
2、c++标准库里的排序函数的使用方法
1)Sort函数包含在头文件为sort函数的头文件是algorithm的c++标准库中
2)sort函数的格式:**sort(start,end,排序方法) **;
三个参数:
(1)第一个是要排序的数组的起始地址。
(2)第二个是结束的地址(最后一位要排序的地址)
(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,默认的排序方法是
从小到大排序
排序方法中的参数类型是待排序数组元素的类型,如:
整数数组,参数就是int a,int b;
字符串数组,参数类型就是char a,char b;
结构体数组,参数就是Student stu1,Student stu2(Student是结构体名称)
二、sort函数应用举例
(一)实数类型
1、默认排序

#include<iostream>
#include<algorithm>
using namespace std;
int main(){
    
    
	int n = 9;
	int a1[n]={
    
    3,97,23,11,87,65,25,43,59};
	sort(a1,a1+n); 				//默认排序
	return 0;
}

输出结果:从小到大排序
在这里插入图片描述
2、从大到小排序

#include<iostream>
#include<algorithm>
using namespace std;
bool large_small(int a,int b){
    
    			//设置排序方法,从大到小排序
	return a>b;
}
int main(){
    
    
	int n = 9;
	int a1[n]={
    
    3,97,23,11,87,65,25,43,59};
	sort(a1,a1+n,large_small); 				//指定排序方法
	return 0;
}

输出结果:
在这里插入图片描述
(二)字符串中的字符排序类型
注意:这里只能用char类型的字符串,不能用string类型
1、默认排序

#include <iostream>
#include <algorithm>
using namespace std;
int main()
{
    
    
    char str[10] ="adebchgfi";
    sort(str, str+ 9);
    for (int i = 0;i < 9;i++)
        cout << str[i];     //输出排序后顺序
    cout << endl;
    return 0;
}

在这里插入图片描述
2、从大到小排序

#include <iostream>
#include <algorithm>
using namespace std;
bool large_and_small(char a,char b){
    
    
	return a>b;
}

int main()
{
    
    
    char str[10] ="adebchgfi";
    sort(str, str+ 9,large_and_small);
    for (int i = 0;i < 9;i++)
        cout << str[i];     //输出排序后顺序
    cout << endl;
    return 0;
}

在这里插入图片描述
(三)结构体排序
例题:
在这里插入图片描述
以这道题为例,我们可以对结构体数组元素进行排序,排序的顺序是,按month排序–>按day排序–>按字符串长短排序–>按字符串大小排序

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Student{
    
    
	string name;
	int month;
	int day;
	int loop;
};
bool Sort_methods(Student stu1,Student stu2){
    
    
	if(stu1.month != stu2.month)
		return stu1.month < stu2.month;								//如果月份不同,则按月份从小到大排序
	else if(stu1.month == stu2.month && stu1.day != stu2.day)
		return stu1.day < stu2.day;									//如果月份相同日子不同,则按日子从小到大排序
	else if(stu1.month == stu2.month && stu1.day == stu2.day && stu1.name.size() != stu2.name.size())
		return stu1.name.size() < stu2.name.size();					//如果月份日子均相同,则按字符串长度从小到大排序
	else
		return stu1.name < stu2.name;								//如果月份日子字符串长度均相同,则按字符串大小从小到大排序
		//注意字符串要用string类型,不能用char数组,因为数组不能直接比较,而string类型的可以
		//char数组比较字典序,比较的是一个字符 
}
int main(){
    
    
	int n;
	cin>>n;
	Student stu[n];
	int index = 0;
	for(int i=0;i<n;i++){
    
    
		cin>>stu[i].name>>stu[i].month>>stu[i].day;
		stu[i].loop = 0;
	}
	sort(stu,stu+n,Sort_methods);
	for(int i=0;i<n;i++)
		cout<<stu[i].month<<" "<<stu[i].day<<" "<<stu[i].name<<endl;
	return 0;	
}

输入:
6
Avril 3 2
Candy 4 5
Tim 3 3
Sufia 4 4
Lagrange 4 6
Bill 3 4

输出:
在这里插入图片描述
上一例题的源码:

#include<iostream>
#include<string>
#include<algorithm>
using namespace std;
struct Student{
    
    
	string name;
	int month;
	int day;
	int loop;
};
bool Sort_methods(Student stu1,Student stu2){
    
    
	if(stu1.month != stu2.month)
		return stu1.month < stu2.month;
	else if(stu1.month == stu2.month && stu1.day != stu2.day)
		return stu1.day < stu2.day;
	else if(stu1.month == stu2.month && stu1.day == stu2.day && stu1.name.size() != stu2.name.size())
		return stu1.name.size() < stu2.name.size();
	else
		return stu1.name < stu2.name;		
}

int main(){
    
    
	int n;
	cin>>n;
	Student stu[n];
	int index = 0;
	for(int i=0;i<n;i++){
    
    
		cin>>stu[i].name>>stu[i].month>>stu[i].day;
		stu[i].loop = 0;
	}
	sort(stu,stu+n,Sort_methods)for(int i=0;i<n;i++){
    
    
		if(stu[i].month == stu[i+1].month && stu[i].day == stu[i+1].day && stu[i].loop == 0){
    
    
			cout<<stu[i].month<<" "<<stu[i].day<<" "<<stu[i].name<<" ";
			stu[i].loop = 1;
			for(int j=i+1;j<n;j++){
    
    
				if(stu[i].month == stu[j].month && stu[i].day == stu[j].day && stu[j].loop == 0){
    
    
					stu[j].loop = 1;
					cout<<stu[j].name<<" ";
				}else
					break;
			}
			cout<<endl;
		}else{
    
    
			stu[i].loop = 1;
			index++;
		}
	}
	if(index == 0 || index == n)
		cout<<"None"<<endl;
	return 0;	
}

猜你喜欢

转载自blog.csdn.net/Cristiano_san/article/details/107141581