排序算法 sort

用sort进行排序(用法一)

对基本类型的数组从小到大排序: sort(数组名+n1,数组名+n2);
n1和n2都是int类型的表达式,可以包含变量 。如果n1=0,则 + n1可以不写将数组中下标范围为[n1,n2)的元素从小到大排序。即下标为n2的元素不在排序区间内。

用sort进行排序(用法一)

int a[] = {15,4,3,9,7,2,6};
sort(a,a+7); //对整个数组从小到大排序

int a[] = {15,4,3,9,7,2,6};
sort(a,a+3); // 结果:{3,4,15,9,7,2,6}

int a[] = {15,4,3,9,7,2,6};
sort(a+2,a+5); //结果:{15,4,3,7,9,2,6}

用sort进行排序(用法二)

对元素类型为T的基本类型数组从大到小排序:
sort(数组名+n1,数组名+n2,greater());

    int a[] = {15,4,3,9,7,2,6};
    
    sort(a+1,a+4,greater<int>()); // 结果:{15,9,4,3,7,2,6}

用sort进行排序(用法三)

用自定义的排序规则,对任何类型T的数组排序
sort(数组名+n1,数组名+n2,排序规则结构名());
排序规则结构的定义方式:

    struct 结构名
    {
    bool operator()( const T & a1,const T & a2) {
    //若a1应该在a2前面,则返回true。
    //否则返回false。
    }
    };
    
    #include <iostream>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    
    struct Rule1 //按从大到小排序
    {
    bool operator()( const int & a1,const int & a2) {
    return a1 > a2;
    }
    };
    struct Rule2 //按个位数从小到大排序
    {
    bool operator()( const int & a1,const int & a2) { return a1%10 < a2%10;
    }
    };
    
    void Print(int a[],int size) { for(int i = 0;i < size;++i)
    cout << a[i] << "," ;
    cout << endl;
    }
    
    int main()
    {
    int a[] = { 12,45,3,98,21,7};
    sort(a,a+sizeof(a)/sizeof(int)); //从小到大
    cout << "1) "; Print(a,sizeof(a)/sizeof(int)); sort(a,a+sizeof(a)/sizeof(int),Rule1()); //从大到小
    cout << "2) "; Print(a,sizeof(a)/sizeof(int));
    sort(a,a+sizeof(a)/sizeof(int),Rule2()); //按个位数从小到大
    cout << "3) "; Print(a,sizeof(a)/sizeof(int));
    
    return 0;
    }

1) 3,7,12,21,45,98,
2) 98,45,21,12,7,3,
3) 21,12,3,45,7,98,

用sort对结构数组进行排序(用法三)

    #include <iostream>
    #include <cstring>
    #include <algorithm> using namespace std; 
	
	struct Student {
    char name[20]; int  id;  double gpa;
    };
    
    Student students [] = {
    {"Jack",112,3.4},{"Mary",102,3.8},{"Mary",117,3.9},
    {"Ala",333,3.5},{"Zero",101,4.0}};
    
    struct StudentRule1 { //按姓名从小到大排
    bool operator() (const Student & s1,const Student & s2) {
    if( stricmp(s1.name,s2.name) < 0)
    return true; return false;
    }
    };
    
    struct StudentRule2 { //按id从小到大排
    bool operator() (const Student & s1,const Student & s2) { return s1.id < s2.id;
    }
    };
    struct StudentRule3 {//按gpa从高到低排
    bool operator() (const Student & s1,const Student & s2) { return s1.gpa > s2.gpa;
    }
    };
    
    void PrintStudents(Student s[],int size){
    for(int i = 0;i < size;++i)
    cout << "(" << s[i].name << ","
    << s[i].id <<"," << s[i].gpa << ") " ;
    cout << endl;
    }
    
    int main()
    {
    int n = sizeof(students) / sizeof(Student);
    sort(students,students+n,StudentRule1()); //按姓名从小到大排
    PrintStudents(students,n); sort(students,students+n,StudentRule2()); //按id从小到大排
    PrintStudents(students,n);
    sort(students,students+n,StudentRule3()); //按gpa从高到低排
    PrintStudents(students,n); return 0;
    }

三种排序的结果:
(Ala,333,3.5) (Jack,112,3.4) (Mary,102,3.8) (Mary,117,3.9)(Zero,101,4)
(Zero,101,4) (Mary,102,3.8) (Jack,112,3.4) (Mary,117,3.9) (Ala,333,3.5)
(Zero,101,4) (Mary,117,3.9) (Mary,102,3.8) (Ala,333,3.5)(Jack,112,3.4)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值