posts - 8,  comments - 11,  trackbacks - 0

          package sort;
          import java.io.*;
          import java.util.*;
          /*
           * @author  NoGodMe
           * @function  使用冒泡法排序
           */
          public class SortBubble {

           public static void main(String[] args) throws IOException {
            SortBubble mp = new SortBubble();
            int[] intBeforeSort = mp.getIntData();
            int[] intAfterSort = mp.sortWithBubble(intBeforeSort);
            for(int i:intAfterSort){
             System.out.println(i);
            }
           }
           
           //獲取從命令行輸入的N個數(shù)字,以-1作為結(jié)束標(biāo)志,并且-1不計算在內(nèi)
           public int[] getIntData() throws IOException{
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            List<Integer> intList = new ArrayList<Integer>();
            String temp = null;
            while( (temp = br.readLine()) != null ){
             if(temp.length() == 0) continue;
             int i = Integer.parseInt(temp.trim());
             if(i == -1) break;
             intList.add(i);
            }
            
            int[] intArr = new int[intList.size()];
            int count = 0;
            for(Integer i:intList){
             intArr[count++] = i.intValue();
            }
            
            return intArr;
           }
           
           //冒泡排序法
           public int[] sortWithBubble(int[] data){
            int temp;
            boolean isChange = false;
            for(int i = 0 ; i < data.length - 1 ; i++){
             isChange = false;
             for(int j = 0 ; j < data.length - 1; j++){
              if(data[j] > data[j + 1]){
               temp = data[j];
               data[j] = data[j + 1];
               data[j + 1] = temp;
               isChange = true;
              }
             }
             if(!isChange) break;
            }
            return data;
           }
           
          }

          posted on 2007-09-28 00:14 詩語江南 閱讀(313) 評論(0)  編輯  收藏 所屬分類: Core JAVA
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          友情連接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 尉氏县| 巫山县| 揭西县| 八宿县| 胶州市| 邯郸市| 金华市| 汝阳县| 习水县| 绥滨县| 邻水| 宣武区| 道孚县| 图片| 克什克腾旗| 夏河县| 彭水| 松原市| 彭阳县| 博爱县| 辽中县| 正宁县| 隆林| 瑞昌市| 乌鲁木齐县| 襄垣县| 玉门市| 龙江县| 建始县| 藁城市| 大冶市| 托克逊县| 宁化县| 丹东市| 新泰市| 宁明县| 瓮安县| 靖边县| 元谋县| 民乐县| 布尔津县|