概述
package study.test08;
import java.util.Scanner;
public class paixu {
public static void main(String[] args) {
demo01();
}
public static void demo01(){
Scanner scanner = new Scanner(System.in);
System.out.print("x:");
int x = scanner.nextInt();
System.out.print("y:");
int y=scanner.nextInt();
System.out.print("z:");
int z=scanner.nextInt();
if(x>y) {
if(x>z){
if(y>z){
System.out.println(z+"<"+y+"<"+x);
return;
}else{
System.out.println(y+"<"+z+"<"+x);
return;
}
}else{
System.out.println(y+"<"+x+"<"+z);
return;
}
}
if(x>z){
if(z>y){
System.out.println(y+"<"+z+"<"+x);
return;
}else{
if(x>y){
System.out.println(z+"<"+y+"<"+x);
return;
}else{
System.out.println(z+"<"+x+"<"+y);
return;
}
}
}
if(y>z){
if(z>x){
System.out.println(x+"<"+z+"<"+y);
return;
}else{
if(x>y){
System.out.println(z+"<"+y+"<"+x);
return;
}else {
System.out.println(z+"<"+x+"<"+y);
return;
}
}
}
}
}
暴力求解,分别进行逐一比较
package study.test08;
import java.util.Scanner;
public class paixu1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("x:");
int x = scanner.nextInt();
System.out.print("y:");
int y = scanner.nextInt();
System.out.print("z:");
int z = scanner.nextInt();
int[] paiXu = {x, y, z};
for (int i = 0; i < paiXu.length; i++) {
for (int j = 0; j < paiXu.length - i-1 ; j++) {
int temp = 0;
if (paiXu[j] > paiXu[j + 1]) {
temp = paiXu[j];
paiXu[j] = paiXu[j + 1];
paiXu[j + 1] = temp;
}
}
}
for (int k = 0; k <3 ; k++) {
System.out.println(paiXu[k]);
}
}
}
以上是冒泡排序求解
package study.test08;
import java.util.Scanner;
public class paixu2 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("x:");
int x = scanner.nextInt();
System.out.print("y:");
int y = scanner.nextInt();
System.out.print("z:");
int z = scanner.nextInt();
int[] paiXu = {x, y, z};
for (int i = 0; i < paiXu.length; i++) {
for (int j = 0; j < paiXu.length - i-1 ; j++) {
if (paiXu[j]>paiXu[j+1]){
paiXu[j]=paiXu[j]+paiXu[j+1];
paiXu[j+1]=paiXu[j]-paiXu[j+1];
paiXu[j]=paiXu[j]-paiXu[j+1];
}
}
}
for (int k = 0; k <3 ; k++) {
System.out.println(paiXu[k]);
}
}
}
不采用中间变量的冒泡排序 记住一加两减
比如a1=15,a2=10
a1=10+15=25
a2=25-10=15
a1=25-15=10
这时a1=10,a2=15
排序完成
最后
以上就是阔达乐曲为你收集整理的键盘输入三个数比较大小(由小到大) 冒泡排序&暴力求解&不采用中间变量的冒泡排序的全部内容,希望文章能够帮你解决键盘输入三个数比较大小(由小到大) 冒泡排序&暴力求解&不采用中间变量的冒泡排序所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复