public class 冒泡 {
public static void main(String[] args) {
int score[] = {6,3,8,2,9,1};
BubbleSort(score);
}
public static void BubbleSort(int[] score){
System.out.println("冒泡前:");
for (int i = 0; i < score.length; i++) {
System.out.print(score[i] + "\t");
}
System.out.println();
int len = score.length;
for (int i = len; i > 1; i--) {//3个数进行排序,只需要进行2趟就可以
for (int j = 0; j < i - 1; j++) {
if(score[j] > score[j+1]){
int temp = score[j];
score[j] = score[j+1];
score[j+1] = temp;
}
}
}
System.out.println("冒泡后:");
for (int i = 0; i < score.length; i++) {
System.out.print(score[i] + "\t");
}
}
}