冒泡排序原理,下面以升序為例
1 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.新聞熱點
疑難解答