冒泡排序

Java 冒泡排序代碼  

public class BubbleTest {

 
 public static void main(String[] args) {
  // 整數類型數組(原始數據類型),隨便定義
  int[] numbs = { 87, 25, 4, 22, 2, 56, 11, 28, 35, 15 };

  // 定義一個臨時交換變量
  int temp = 0;
  for (int i = 0; i < numbs.length; i++) {
   for (int j = 0; j < numbs.length - 1; j++) {
    if (numbs[i] < numbs[j]) {
     // 沒有動作
    } else {
     temp = numbs[i];
     numbs[i] = numbs[j];
     numbs[j] = temp;
    }
   }
  }

  // 打印冒泡排序過後的數組
  for (int i = 0; i < numbs.length; i++) {
   System.out.println(numbs[i]);
  }
 }

}

 

結果:

87
56
35
28
25
22
15
11
4
2


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章