用冒泡排序对数组 arry[1,796,45,46,58,22]进行排序
数字小的排在后面
public class arry {
public static void main(String[] args) {
int arry[] = {1,796,45,46,58,22,};
int temp = 0;
for (int j = 0; j < arry.length-1; j++) {
for (int i = 0; i < arry.length-1-j; i++) {
if (arry[i] > arry[i+1]){
temp = arry[i];
arry[i] = arry[i+1];
arry[i+1] = temp;
}
}
}
for (int i = 0; i < arry.length; i++){
System.out.println(arry[i]);
}
}
}
数字大的排在前面
public class arry {
public static void main(String[] args) {
int arry[] = {1,796,45,46,58,22,};
int temp = 0;
for (int j = 0; j < arry.length-1; j++) {
for (int i = 0; i < arry.length-1-j; i++) {
if (arry[i] < arry[i+1]){
temp = arry[i+1];
arry[i+1] = arry[i];
arry[i] = temp;
}
}
}
for (int i = 0; i < arry.length; i++){
System.out.println(arry[i]);
}
}
}
键盘输入数字进行数组冒泡排序
import java.util.Scanner;
public class arry {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
System.out.println("输入你要冒泡排序的个数");
int a = scanner.nextInt();
int arry[]=new int[a];
// 将输入的数据存入数组中
System.out.println("输入需要排序的数字");
for (int i = 0;i <=arry.length-1; i++){
int b = scanner.nextInt();
arry[i] = b;
if (i> arry.length){
break;
}
}
int temp = 0;
for (int j = 0; j < arry.length-1; j++) {
for (int i = 0; i < arry.length-1-j; i++) {
if (arry[i] < arry[i+1]){
temp = arry[i+1];
arry[i+1] = arry[i];
arry[i] = temp;
}
}
}
for (int i = 0; i < arry.length; i++){
System.out.println(arry[i]);
}
}
}