三位整数从小到大排序

题目描述

给出三个整数 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值