Johnny

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

          冒泡排序Java代碼


          /**
           * 冒泡排序
           * 
          @author sikaijian
           
          */
          public class BubbleSort {
              public static void sort(int[] data){
                  if(data.length<=1) return;
                  
                  /**
          * 每一趟排序都把最大的數(shù)字放到最后
          * 下一趟排序后,最大的數(shù)不參加
          * 總共n-1趟(n為數(shù)組長度)
          */
                  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 瓢菝的雨夜 閱讀(137) 評論(0)  編輯  收藏 所屬分類: 數(shù)據(jù)結(jié)構(gòu)算法


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


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 茶陵县| 武义县| 会同县| 墨玉县| 安国市| 灵山县| 南投市| 萝北县| 彭州市| 镇沅| 武乡县| 仙居县| 保德县| 和田市| 英超| 祁连县| 泗水县| 射阳县| 景谷| 邮箱| 蒙山县| 昔阳县| 荔波县| 孟津县| 页游| 六枝特区| 宁国市| 文化| 湘乡市| 米脂县| 榆社县| 达州市| 玉门市| 辉南县| 云安县| 广南县| 喀什市| 城固县| 平顺县| 车致| 康乐县|