C语言qsort
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
void print(int *array,int n){
for(int i=0;i<n;i++){
cout<<array[i]<<" ";
}
cout<<endl;
}
int cmp(const void* a,const void* b){
return *((int*)a)-*((int*)b);
}
int main(){
int array[]={11,222,3,44,55,666,-100,1};
qsort(array,8,sizeof(int),cmp);
print(array,8);
return 0;
}
c++
#include<iostream>
#include<algorithm>
using namespace std;
bool cmp(int a,int b){
return a>b;
}
int main(){
int array[]={11,222,3,44,55,666,-100,1};
// sort(array,array+8,cmp);//
// sort(array,array+8);//默认小--->大less
// sort(array,array+8,greater<int>());//大--->小 greater
sort(array,array+8,[](int a,int b){return a>b;});
// sort(v.begin(),v.end(),greater<int>());
for(auto x:array) cout<<x<<" ";
return 0;
}