Johnny

          表面的激烈是由于內心的單薄,真正的力量如同流水一般沉靜
          隨筆 - 1, 文章 - 5, 評論 - 0, 引用 - 0
          數據加載中……

          冒泡排序Java代碼


          /**
           * 冒泡排序
           * 
          @author sikaijian
           
          */
          public class BubbleSort {
              public static void sort(int[] data){
                  if(data.length<=1) return;
                  
                  /**
          * 每一趟排序都把最大的數字放到最后
          * 下一趟排序后,最大的數不參加
          * 總共n-1趟(n為數組長度)
          */
                  for (int i = data.length-1; i > 0; i--) {
                      for (int j = 0; j < i; j++) {
                          if(data[j]>data[j+1]){
                              int temp = data[j];
                              data[j] = data[j+1];
                              data[j+1] = temp;
                          }
                      }
                  }
              }
              
              /**
               * 測試代碼
               * 
          @param args
               
          */
              public static void main(String[] args) {
                  int[] data = new int[] { 49, 23, 65, 13, 38, 96, 12, 33, 88, 123, 22,
                          11, 9, 55, 111, 0 };

                  for (int t : data) {
                      System.out.print(t);
                      System.out.print(" ");
                  }
                  System.out.println();
                  System.out.println("------------------------------");
                  BubbleSort.sort(data);

                  for (int t : data) {
                      System.out.print(t);
                      System.out.print(" ");
                  }
              }
          }

          posted on 2012-09-21 13:32 瓢菝的雨夜 閱讀(135) 評論(0)  編輯  收藏 所屬分類: 數據結構算法


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 通渭县| 额尔古纳市| 凤阳县| 英山县| 阿坝县| 凤凰县| 溧水县| 临武县| 特克斯县| 宜章县| 蓝山县| 景泰县| 安图县| 吴川市| 蕉岭县| 吉安市| 锦州市| 黄浦区| 东光县| 丁青县| 平安县| 两当县| 渝北区| 兴山县| 调兵山市| 丰台区| 西和县| 阿拉善左旗| 景谷| 庆云县| 凤山市| 古丈县| 玛沁县| 岢岚县| 鲁山县| 信宜市| 大英县| 紫阳县| 无极县| 呼伦贝尔市| 甘谷县|