冒泡排序:算法
冒泡排序(Bubble Sort)是一種簡單的排序算法。spa
冒泡排序算法的運做以下:code
public class BubbleSort{ 2 public static void main(String[] args){ 3 int score[] = {67, 69, 75, 87, 89, 90, 99, 100}; 4 for (int i = 0; i < score.length -1; i++){ //最多作n-1趟排序 5 for(int j = 0 ;j < score.length - i - 1; j++){ //對當前無序區間score[0......length-i-1]進行排序(j的範圍很關鍵,這個範圍是在逐步縮小的) 6 if(score[j] < score[j + 1]){ //把小的值交換到後面 7 int temp = score[j]; 8 score[j] = score[j + 1]; 9 score[j + 1] = temp; 10 } 11 } 12 System.out.print("第" + (i + 1) + "次排序結果:"); 13 for(int a = 0; a < score.length; a++){ 14 System.out.print(score[a] + "\t"); 15 } 16 System.out.println(""); 17 } 18 System.out.print("最終排序結果:"); 19 for(int a = 0; a < score.length; a++){ 20 System.out.print(score[a] + "\t"); 21 } 22 } 23 }