冒泡排序的过程
排序次数是外层循环(5-1)
交换次数是内层循环(5-i-1)
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
int arr[] = new int[5];
int temp;
System.out.println("请输入5名学生的成绩:");
Scanner input = new Scanner(System.in);
for (int i = 0; i < 5; i++) {
arr[i] = input.nextInt();
}
for (int m = 0; m < 5; m++) {
for (int n = 0; n < 5-1-m; n++) {
if(arr[n]<arr[n+1]){ //这行代码是用来规定是升序还是降序排列
temp = arr[n];
arr[n] = arr[n+1];
arr[n+1] = temp;
}
}
}
System.out.println("学员成绩按降序排列:");
for (int j = 0; j < 5; j++) {
System.out.print(arr[j]+"\t");
}
}
}
运行结果:
标签:arr,JAVA,Scanner,int,冒泡排序,++,public From: https://blog.csdn.net/m0_71192988/article/details/141106037