题目描述
给出三个整数 a,b,c(0≤a,b,c≤100)a,b,c(0\le a,b,c \le 100)a,b,c(0≤a,b,c≤100),要求把这三位整数从小到大排序。
输入格式
无
输出格式
无
输入输出样例
输入 #1
1 14 5
输出 #1
1 5 14
输入 #2
2 2 2
输出 #2
2 2 2
一、快排
#include<bits/stdc++.h>
using namespace std;
int s[3];
int main(){
cin>>s[0]>>s[1]>>s[2];
sort(s,s+3);//表示排序p数组的[0到2]位置,注意sort默认从小到大排
cout<<s[0]<<' '<<s[1]<<' '<<s[2];
return 0;
}
二、比较交换
通过三次比较交换来排序:
#include<bits/stdc++.h>
using namespace std;
int main(){
int a,b,c;
if(a>b) swap(a,b);
if(a>c) swap(a,c);
if(b>c) swap(b,c);
cout<<a<<' '<<b<<' '<<c;
return 0;
}
三、桶排
#include<bits/stdc++.h>
using namespace std;
map<int,int> s;
int mx=0;
int main(){
for(int i=1;i<=3;i++){
int a;
cin>>a;
mx=max(mx,a);//小优化
s[a]++;
}
for(int i=0;i<=mx;i++){
if(s[i]) cout<<i<<' ';
}
return 0;
}
四、堆排
#include<bits/stdc++.h>
using namespace std;
priority_queue<int, vector<int >,greater<int> >q2;//小根堆
int main() {
int n=3;
int s;
for(int i=1; i<=n; i++) {
cin>>s;
q2.push(s);
}
for(int i=1; i<=n; i++) {
cout<<q2.top()<<' ';
q2.pop();
}
return 0;
}