补充排序函数(更快速)

补充排序函数(更快速)

1、sort函数可以三个参数也可以两个参数,必须的头文件#include < algorithm>和using namespace std;
2、它使用的排序方法是类似于快排的方法,时间复杂度为n*log2(n)

3、Sort函数有三个参数:(第三个参数可不写)

(1)第一个是要排序的数组的起始地址。

(2)第二个是结束的地址(最后一位要排序的地址)

(3)第三个参数是排序的方法,可以是从大到小也可是从小到大,还可以不写第三个参数,此时默认的排序方法是从小到大排序。

两个参数用法

#include <iostream>
#include <algorithm>
int main()
{
 int a[20]={2,4,1,23,5,76,0,43,24,65},i;
 for(i=0;i<20;i++)
  cout<<a[i]<<endl;
 sort(a,a+20);
 for(i=0;i<20;i++)
 cout<<a[i]<<endl;
 return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
输出结果是升序排列。(两个参数的sort默认升序排序)

三个参数
// sort algorithm example

#include <iostream>     // std::cout
#include <algorithm>    // std::sort
#include <vector>       // std::vector

bool myfunction (int i,int j) { return (i<j); }//升序排列
bool myfunction2 (int i,int j) { return (i>j); }//降序排列

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {
    int myints[8] = {32,71,12,45,26,80,53,33};
  std::vector<int> myvector (myints, myints+8);               // 32 71 12 45 26 80 53 33

  // using default comparison (operator <):
  std::sort (myvector.begin(), myvector.begin()+4);           //(12 32 45 71)26 80 53 33

  // using function as comp
  std::sort (myvector.begin()+4, myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)
    //std::sort (myints,myints+8,myfunction);不用vector的用法

  // using object as comp
  std::sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

  // print out content:
  std::cout << "myvector contains:";
  for (std::vector<int>::iterator it=myvector.begin(); it!=myvector.end(); ++it)//输出
    std::cout << ' ' << *it;
  std::cout << '\n';

  return 0;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
string 使用反向迭代器来完成逆序排列

#include <iostream>
using namespace std;
int main()
{
     string str("cvicses");
     string s(str.rbegin(),str.rend());
     cout << s <<endl;
     return 0;
}

//输出:sescivc

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/114925.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【转载】WebService相关概念

    【转载】WebService相关概念

    2021年11月18日
    65
  • Qt的下载安装全教程

    Qt的下载安装全教程Qt的安装及环境配置

    2022年5月17日
    56
  • mysql设置隔离级别_修改mysql事务隔离级别

    mysql设置隔离级别_修改mysql事务隔离级别引言开始我们的内容,相信大家一定遇到过下面的一个面试场景面试官:“讲讲mysql有几个事务隔离级别?”你:“读未提交,读已提交,可重复读,串行化四个!默认是可重复读”面试官:“为什么mysql选可重复读作为默认的隔离级别?”(你面露苦色,不知如何回答!)面试官:”你们项目中选了哪个隔离级别?为什么?”你:“当然是默认的可重复读,至于原因。。呃。。。”(然后你就可以回去等通知了!)为了避免上述尴尬的…

    2025年8月27日
    12
  • 利用JDK发布webService实例「建议收藏」

    利用JDK发布webService实例「建议收藏」一、webService的发布1、新建一个webProject2、修改jdk为1.6及以上—-3、编写方法—- packagecom.test.webService;importjavax.jws.WebService;importjavax.xml.ws.Endpoint;@WebServicepublicclass

    2022年7月21日
    18
  • PyCharm常用设置(图解)

    PyCharm常用设置(图解)1.保存设置pycharm中的设置是可以导入和导出的,file>exportsettings可以保存当前pycharm中的设置为jar文件保存在桌面上2.导入设置重装时可以直接importsettings>jar文件,就不用重复配置了确认是否要导入点击确认重新启动3.设置Python自动引入包设置Python自动引入包,要先在…

    2022年8月27日
    7
  • Python requests post 请求报错:415 Unsupported Media Type

    Python requests post 请求报错:415 Unsupported Media Type本文仅供学习交流使用,如侵立删!联系方式及demo下载见文末requestspost请求报错:415UnsupportedMediaType在使用response.post发送json数据时,出现如题所示错误,是因为User-Agent被服务器设置拒绝请求了解决方法:’content-type’:’application/json’headers={‘User-Agent’:’Dalvik/2.1.0(Linux;U;Android6.0.1;Nexus5

    2022年6月12日
    92

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号