冒泡排序

Java 冒泡排序代碼  

數組

public class BubbleTest {ide

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

  // 定義一個臨時交換變量
  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;
    }
   }
  }orm

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

}it

 

結果:class

87
56
35
28
25
22
15
11
4
2變量

相關文章
相關標籤/搜索
本站公眾號
   歡迎關注本站公眾號,獲取更多信息