C++排序與sort

對於一個連續存儲空間的數組進行排序,我們可以使用C++中提供 的sort。
C++中已經爲我們寫好了sort函數,需要調用是我們要引入一個頭文件

#inlcide<algorithm>

要提醒的是sort可以排序任何類型的元素,包括自行定義的結構體

sort用例

int main()
{
	int arr[] = {0,-3,7,9,1,4};
	//對arr[0]到arr[5]進行排序,按照從大到小的順序排列
	sort(arr+0,arr+6,greater<int>());
	for(int i=0;i<6;i++)
		cout<<arr[i]<<" ";
	return 0;
}

在這裏插入圖片描述

使用cmp函數

cmp爲bool函數,可以自定義sort排序的規則。
如果不重寫cmp函數,sort函數默認爲從小到大排序。

從小到大排序

//升序排序
bool cmp(int x,int y)
{
	return x < y; 
}

從大到小排序

//降序排序
bool cmp(int x,int y)
{
	return x > y;
}

有N個學生的數據,要進行升序排序。將學生數據按成績高低排序,如果成績相同則按姓名字符的字母序排序,如果姓名的字母序也相同則按照學生的年齡排序,並輸出N個學生排序後的信息。

typedef struct
{
	int score;
	string name;
	int age;
}student;
bool cmp(student a, student b)
{
	if(a.score != b.score)
		return a.score < b.score;
	else if(a.name != b.name)
		return a.name < b.name;
	else
		return a.age < b.age;
}

例題

在這裏插入圖片描述來源:http://acm.nefu.edu.cn/problemShow.php?problem_id=2068

//這裏只重寫cmp比較函數
typedef struct
{
	string name;
	int year;
	int month;
	int day;
	int position;
}stu;
bool cmp(stu a,stu b)
{
	if(a.year != b.year)
		return a.year < b.year;
	else if(a.month != b.month)
		return a.month < b.month;
	else if(a.day != b.day)
		return a.day < b.day;
	else
		return a.position > b.position;
}

數組的sort排序

test1

在這裏插入圖片描述

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
using namespace std;
int a[31];
int main()
{
	int N, k;
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>a[i];
	cin>>k;
	sort(a+0,a+N,greater<int>());//降序排序
	double ave = 0;
	for(int i=0;i<k;i++)
		ave += a[i];
	ave /= k;
	printf("%.2f",ave);
	return 0;
}

test2

在這裏插入圖片描述

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[31];
int b[7];
int main()
{
	memset(b,0,sizeof(b));
	int N;
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>a[i];
	sort(a+0,a+N,greater<int>());//降序排序
	for(int i=0;i<N;i++){
		if(a[i] == 100)
			b[1]++;
		else if(a[i]>=90 && a[i] <= 99)
			b[2]++;
		else if(a[i]>=80 && a[i] <= 89)
			b[3]++;
		else if(a[i]>=70 && a[i] <= 79)
			b[4]++;
		else if(a[i]>=60 && a[i] <= 69)
			b[5]++;
		else
			b[6]++;
	}
	//輸出
	for(int i=0;i<N;i++)
		cout<<a[i]<<endl;
	for(int i=1;i<6;i++)
		cout<<b[i]<<" ";
	cout<<b[6];
	return 0;
}

test3

在這裏插入圖片描述

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<algorithm>
#include<string.h>
using namespace std;
int a[105];
bool cmp(int x,int y)
{
	if(x%3 != y%3)//按照餘數從小到大排序
		return x%3 < y%3;
	else//餘數相等,按照正整數從小到大排序
		return x<y;
}
int main()
{
	int N;
	cin>>N;
	for(int i=0;i<N;i++)
		cin>>a[i];
	sort(a+0,a+N,cmp);
	for(int i=0;i<N;i++)
		cout<<a[i]<<" ";
	return 0;
}

結構體的sort排序

一個程序中可以定義多種排序方法,自行定義函數名並調用即可。

test

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

struct Student {
    string name;
    int score[4];
};
//按照姓名字典序升序排列
bool cmp_name(Student x, Student y) {
    return x.name < y.name;
}
//先按照第一門課的成績降序排列,
//如果第一門課成績相同,按照第二門課成績降序排列
//如果第二門課成績相同,按照第三門課成績降序排列,依此類推。
bool cmp_score(Student x, Student y) {
    if (x.score[0] != y.score[0]) {
        return x.score[0] > y.score[0];
    }
    if (x.score[1] != y.score[1]) {
        return x.score[1] > y.score[1];
    }
    if (x.score[2] != y.score[2]) {
        return x.score[2] > y.score[2];
    }
    return x.score[3] > y.score[3];
}
int main() {
    Student stu[3];
    for (int i = 0; i < 3; i++) {
        cin >> stu[i].name;
        for (int j = 0; j < 4; j++) {
            cin >> stu[i].score[j];
        }
    }
    sort(stu, stu +3, cmp_name);
    for (int i = 0; i < 3; i++) {
        cout << stu[i].name << ": ";
        for (int j = 0; j < 4; j++) {
            cout << stu[i].score[j] << " ";
        }
        cout << endl;
    }
    sort(stu, stu + 3, cmp_score);
    for (int i = 0; i < 3; i++) {
        cout << stu[i].name << ": ";
        for (int j = 0; j < 4; j++) {
            cout << stu[i].score[j] << " ";
        }
        cout << endl;
    }
    return 0;
}

由於sort排序這個知識點比較簡單,不再贅述。
筆者認爲sort函數的重點在於如何cmp函數

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章