網站首頁 編程語言 正文
一、sort 的介紹
sort是c++ algorithm 庫里的一個排序函數。排序太常用了,如果每次都要自己寫排序函數的話會浪費程序員大量的時間和精力,所以函數庫里就寫好了一些排序算法以供我們使用。
sort()是不穩定的排序,底層使用的是快速排序算法,平均時間復雜度為O(n*log n)
如果需要穩定排序可以使用stable_sort(),底層使用歸并排序實現的,時間復雜度固定是O(n*log n)
sort()和stable_sort()用法是一樣的,下面我們只講解sort()的使用
二、sort的基本用法
sort(起始地址,末尾地址+1);
sort(起始地址,末尾地址+1,cmp);
sort是默認升序排序的,如果需要自定義排序,可以寫一個比較函數,用第二種方法排序
1、普通數組的排序
示例代碼:
#include<iostream>
#include<algorithm> //使用sort等算法函數需要的頭文件
using namespace std;
void print(int a[]){//打印函數
for(int i=0;i<10;i++){
cout<<a[i]<<' ';
}
cout<<endl;
}
bool cmp(int a1,int a2){//大于號是升序排序,小于號是降序排序
return a1>a2;
}
int main(){
int a[10]={3,1,4,5,8,0,9,2,7,6};
cout<<"排序前:"<<endl;
print(a); //打印
cout<<endl;
sort(a,a+10);//排序,默認是升序的
cout<<"sort(a,a+10)排序后:"<<endl;
print(a); //打印
cout<<endl;
sort(a,a+10,cmp);//自定義排序
cout<<"sort(a,a+10,cmp)自定義降序排序后:"<<endl;
print(a);
}
運行結果:
排序前:
3 1 4 5 8 0 9 2 7 6
sort(a,a+10)排序后:
0 1 2 3 4 5 6 7 8 9sort(a,a+10,cmp)自定義降序排序后:
9 8 7 6 5 4 3 2 1 0
2、結構體的排序
因為結構體默認是沒有比較大小的功能的,所以我們必須使用cmp函數定義排序方法
示例代碼:
#include<iostream>
#include<algorithm> //使用sort等算法函數需要的頭文件
using namespace std;
struct test{
int a;
int b;
};
bool cmp1(test t1,test t2){//先按a升序排序,再按b升序排序
if(t1.a==t2.a){
return t1.b<t2.b;
}
return t1.a<t2.a;
}
bool cmp2(test t1,test t2){//先按a降序排序,再按b降序排序
if(t1.a==t2.a){
return t1.b>t2.b;
}
return t1.a>t2.a;
}
void print(test t[]){
for(int i=0;i<5;i++){
cout<<"t["<<i<<"]("<<t[i].a<<","<<t[i].b<<") ";
}
cout<<endl;
}
int main(){
test t[5];
t[0].a=2;
t[0].b=3;
t[1].a=5;
t[1].b=3;
t[2].a=5;
t[2].b=2;
t[3].a=2;
t[3].b=8;
t[4].a=1;
t[4].b=1;
cout<<"排序前:"<<endl;
print(t); //打印
cout<<endl;
sort(t,t+5,cmp1);//自定義升序排序
cout<<"sort(a,a+5,cmp1)自定義升序排序后:"<<endl;
print(t);
cout<<endl;
sort(t,t+5,cmp2);//自定義降序排序
cout<<"sort(a,a+5,cmp2)自定義降序序排序后:"<<endl;
print(t);
}
運行結果:
排序前:
t[0](2,3) t[1](5,3) t[2](5,2) t[3](2,8) t[4](1,1)sort(a,a+5,cmp1)自定義升序排序后:
t[0](1,1) t[1](2,3) t[2](2,8) t[3](5,2) t[4](5,3)sort(a,a+5,cmp2)自定義降序序排序后:
t[0](5,3) t[1](5,2) t[2](2,8) t[3](2,3) t[4](1,1)
3、vector等數據結構的排序
像vector和string等數據結構,我們排序時是不能像數組那樣直接用名字代表地址來排序的,而必須使用它們的迭代器begin()和end()來完成排序
示例代碼:
#include<iostream>
#include<vector> //使用vector容器需要使用這個頭文件
#include<algorithm> //使用sort等算法函數需要的頭文件
using namespace std;
print(vector<int> v){//打印函數
for(int i=0;i<v.size();i++){
cout<<v[i]<<' ';
}
cout<<endl;
}
int main(){
vector<int> v;//定義一個int型的vector
v.push_back(2);//在尾部插入一個元素2
v.push_back(3);//在尾部插入一個元素3
v.push_back(7);
v.push_back(1);
v.push_back(9);
v.push_back(8);
v.push_back(0);
v.push_back(5);
v.push_back(4);
cout<<"排序前:"<<endl;
print(v);
cout<<endl;
sort(v.begin(),v.end());//用迭代器排序
cout<<"sort(v.begin(),v.end())升序排序后:"<<endl;
print(v);
cout<<endl;
sort(v.rbegin(),v.rend());//反向迭代器可實現降序排序
cout<<"sort(v.rbegin(),v.rend())降序排序后:"<<endl;
print(v);
}
運行結果:
排序前:
2 3 7 1 9 8 0 5 4sort(v.begin(),v.end())升序排序后:
0 1 2 3 4 5 7 8 9sort(v.rbegin(),v.rend())降序排序后:
9 8 7 5 4 3 2 1 0
當然,vector等結構都是可以用cmp函數自定義排序方法的,感興趣的同學可以嘗試一下,這里就不在敘述了,因為都是大同小異的。學會舉一反三學習效率才會高
總結
原文鏈接:https://blog.csdn.net/weixin_52115456/article/details/127613394
相關推薦
- 2022-04-22 mac解決npm不管裝啥都是zsh: command not found
- 2023-01-15 關于networkx返回圖的鄰接矩陣問題_python
- 2022-06-23 C++11中模板隱式實例化與顯式實例化的定義詳解分析_C 語言
- 2022-10-05 Android?Flutter實現原理淺析_Android
- 2022-07-23 C++數據結構之搜索二叉樹的實現_C 語言
- 2022-08-01 C++簡單又輕松的講解類和對象中友元函數_C 語言
- 2023-01-01 Objects?are?not?valid?as?a?React?child報錯解決_React
- 2022-10-12 Go?Excelize?API源碼解析GetSheetFormatPr使用示例_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支