算法之sort排序

sort函数

 包含头文件:algorithm
 sort函数有三个参数:
(1)要排序数组的起始地址;

(2)要排序数组的最后一个数据元素的下一个地址;

(3)排序方法,如果没有排序方法的话,默认从小到大排序;

sort函数实现数的排序

#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a, int b)///实现从大到小排序的比较函数
{
    
    
    return a > b;
}

int main()
{
    
    
    int i, a[100], n;
    while (cin >> n)
    {
    
    
        for (i = 0; i < n; i++)
            scanf("%d", &a[i]);
        sort(a, a + n, cmp);
        for (i = 0; i < n; i++)
            printf("%d ", a[i]);
        cout << endl;
    }
    return 0;
}

sort函数实现字符串的排序

sort函数对string内的字符进行排序(string为字符串类型数据)

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp(char a, char b)///比较函数
{
    
    
    return a > b;
}
int main()
{
    
    
    string s;
    while (cin >> s)
    {
    
    
        sort(s.begin(), s.end());///从小到大
        cout << s << endl;
        sort(s.begin(), s.end(), cmp);///从大到小
        cout << s << endl;
        cout << s.length() << endl;///字符串长度函数
    }
}

sort对一维字符数组内的字符进行排序

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp(char a, char b)///比较函数
{
    
    
    return a > b;
}
int main()
{
    
    
    char a[100];
    while (cin >> a)
    {
    
    
        sort(a, a + strlen(a));///从小到大
        cout << a << endl;
        sort(a, a + strlen(a), cmp);///从大到小
        cout << a << endl;
        cout << strlen(a) << endl;///输出字符数组a的长度
    }
}

sort对string类型的数组按照字典序排序

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp(string a, string b)///按照字典序从大到小排序
{
    
    
    return a > b;
}
int main()
{
    
    
    int i, j;
    string  s[12] =
    {
    
    
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };
    sort(s, s + 12);///字符串默认按照字典序从小到大排序,数默认按照大小从小到大排序
    for (i = 0; i < 12; i++)
        cout << s[i] << endl;
    cout << endl;
    sort(s, s + 12, cmp);///按照字典序从大到小排序
    for (i = 0; i < 12; i++)
        cout << s[i] << endl;
    return 0;
}

sort对string类型的数组根据长度进行排序

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
bool cmp1(string a, string b)///按照长度从小到大排序
{
    
    
    return a.length() < b.length();
}
bool cmp2(string a, string b)///按照长度从大到小排序
{
    
    
    return a.length() > b.length();
}
int main()
{
    
    
    int i, j;
    string  s[12] =
    {
    
    
        "January",
        "February",
        "March",
        "April",
        "May",
        "June",
        "July",
        "August",
        "September",
        "October",
        "November",
        "December"
    };
    sort(s, s + 12, cmp1);///按照长度从小到大排序
    for (i = 0; i < 12; i++)
        cout << s[i] << endl;
    cout << endl;
    sort(s, s + 12, cmp2);///按照长度从大到小排序
    for (i = 0; i < 12; i++)
        cout << s[i] << endl;
    return 0;
}

对结构体数组进行排序(根据结构体中的某元素)

#include<iostream>
#include<cstring>
#include<algorithm>
using namespace std;
const int N = 3;
struct Student
{
    
    
    char name[20];///姓名
    int score;  ///成绩
}stu[100010];
/*
(1)如果两个学生的分数不同,则分数高的排在前面
(2)否则,将姓名字典序小的排在前面
*/
bool cmp(Student a, Student b)
{
    
    
    if (a.score != b.score) return a.score > b.score;
    else return strcmp(a.name, b.name) < 0;
}

int main()
{
    
    
    int i;
    stu[0] = {
    
     "yzh",101 };
    stu[1] = {
    
     "pzj",100 };
    stu[2] = {
    
     "dzc",100 };
    sort(stu, stu + N, cmp);
    for (i = 0; i < N; i++)
        cout << stu[i].name << "  " << stu[i].score << endl;
    cout << endl;
}

Guess you like

Origin blog.csdn.net/qq_52297656/article/details/119851780