網站首頁 編程語言 正文
一、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-09-16 Python獲取時間的操作示例詳解_python
- 2022-11-18 C語言手寫多級時間輪定時器_C 語言
- 2022-10-10 python使用pandas讀寫excel文件的方法實例_python
- 2022-11-28 matplotlib?雙y軸繪制及合并圖例的實現代碼_python
- 2022-07-04 Python+Pillow+Pytesseract實現驗證碼識別_python
- 2022-09-21 Python腳本開發中的命令行參數及傳參示例詳解_python
- 2022-09-03 Go語言函數的延遲調用(Deferred?Code)詳解_Golang
- 2022-04-17 spring cloud config和bus組件實現自動刷新
- 最近更新
-
- 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同步修改后的遠程分支