• 排序函数sort用法简介


    排序算法有很多,冒泡排序,选择排序,堆排序,快速排序,归并排序,基数排序……

    其中平均复杂度O(nlogn)的排序算法或者在某方面有特殊优势的算法在ACM中才有实际使用价值,所以上述提到的前2种大家以后就不要用了。其他排序算法大家会慢慢接触,本文主要介绍使用最多的排序函数 sort。大家可能会遇到qsort,qsort比较复杂,逐渐淡出ACMer的视线,所以不用管它。

    sort函数是C++标准库函数,需要包含头文件 #include <algorithm> 并声明命名空间 using namespace std;

    using namespace std;紧跟在#include <algorithm>之后

    举例:

    #include <stdio.h>
    
    #include <algorithm>
    
    using namespace std;
    
    
    
    int main ()
    
    {
    
      return 0;
    
    }

    排序中有一个重要的概念:稳定排序和不稳定排序。

    稳定排序:大小相等的元素在被排序后前后位置关系不变

    不稳定排序:大小相等的元素在被排序后前后位置关系可能改变

    sort() 是不稳定排序,稳定排序函数是 stable_sort()

    sort() 的排序对象可以是任何数据类型,int , __int64 ,  long long , char , double , string , 结构体……

    1.默认的sort函数是按升序排,这时需要传递两个参数。

    下面这句的作用是对数组元素a[0]~a[n-1]进行升序排序

    sort(a,a+n);   //两个参数分别为待排序数组的首地址和尾地址

    下面这句的作用是对数组元素a[1]~a[n]进行升序排序

    sort(a+1,a+n+1);  //注意体会参数的含义和特点。

    2.可以自己写一个cmp函数,按特定意图进行排序。这时需要传递三个参数,第三个参数为自己定义的比较函数的名字。

    这个自己写的比较函数返回值应该为真(非0)或假(0),传入参数应该有两个,具体写法参照下面的代码。

    这个比较函数的写法很固定,大家不理解可以先背下来。这个函数会在sort函数执行时自动调用。

    比较函数的详细原则补充在最后,看不懂没关系……

    注意比较函数是自己定义的函数,和其他函数一样,写在主函数外面……
    例如:

    对数组a降序排序:

    int cmp ( const int &a, const int &b )
    
    {
        if ( a > b )
           return 1;
        else
           return 0;
    }
    sort(a,a+n,cmp);

    或者写作:

    bool cmp ( const int &a, const int &b )
    
    {
        return a > b;
    }
    
    int mian ()
    {
        sort(a,a+n,cmp);
    }

    又如:先按x升序排序,若x值相等则按y升序排,其中a为结构体数组,其中有x,y两个元素:

    int cmp ( const POINT &a, const POINT &b ) //POINT为自己定义的结构体
    
    {
        if( a.x < b.x )
           return 1;
        else
           if( a.x == b.x ){
              if( a.y < b.y )
                 return 1;
              else
                 return 0;
            }
           else
              return 0;
    }
    sort(a,a+n,cmp);

    或者写作:

    bool  cmp ( const POINT &a, const POINT &b ) //POINT为自己定义的结构体
    
    {
        if (a.x==b.x) return  a.y < b.y ;
        return  a.x < b.x;
    }
    sort(a,a+n,cmp);

    比较函数的详细说明:

    C++STL中的许多东西,例如sort说到底是在利用小于号,'<'来进行比较,所以以后大家会通过重载小于运算符的方法来实现各种功能,现阶段先用比较函数。

    比较函数说到底就是要实现小于号的功能,告诉程序什么样的情况是“小于”。sort()函数会把更小的元素放在前面。

    默认情况下3<5,所以sort()会实现升序排序(小的在前)

    如果我们定义3>5,那么sort()就会把“更小的”5放在前面,于是就实现了降序。

    也就是说,通过对不同的比较函数的定义,就可以随心所欲的实现各种比较。

    再举个例子,对结构体排序

    struct STU
    
    {
    
      char name[20];
    
      int score;
    
    }data[105];
    
    
    
    bool cmp (const STU &a,const STU &b)
    
    {//成绩降序,成绩相同时按姓名升序
    
      if (a.score==b.score) return strcmp(a.name,b.name)<0;
    
      return a.score>b.score;
    
    }
    
    sort(data,data+10,cmp);
  • 相关阅读:
    mybatis 错误 Invalid bound statement (not found)
    Redis is configured to save RDB snapshots, but is currently not able to persist on disk. Commands that may modify the data set are disabled. Please check Redis logs for details about the error.
    bug 记录 Unable to start ServletWebServerApplicationContext due to multiple ServletWebServerFactory beans
    解决:The Tomcat connector configured to listen on port 8182 failed to start. The port may already be in use or the connector may be misconfigured.
    jquery validate 验证插件 解决多个相同的Name 只验证第一个的方案
    phpStorm+xdebug调试(php7.3)
    小程序视频多个视频播放与暂停
    CSS实现单行、多行文本溢出显示省略号(…)
    Packet for query is too large (4,544,730 > 4,194,304). You can change this value on the server by setting the 'max_allowed_packet' variable.
    idea自动在文件头中添加作者和创建时间
  • 原文地址:https://www.cnblogs.com/whyorwhnt/p/3720780.html
Copyright © 2020-2023  润新知