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個數字,以-1作為結束標志,并且-1不計算在內
           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 詩語江南 閱讀(306) 評論(0)  編輯  收藏 所屬分類: Core JAVA
          <2007年9月>
          2627282930311
          2345678
          9101112131415
          16171819202122
          23242526272829
          30123456

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          友情連接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 陇西县| 怀化市| 双城市| 巴南区| 柞水县| 辉南县| 湄潭县| 仁化县| 佛冈县| 鹤山市| 武鸣县| 新巴尔虎左旗| 德庆县| 阿拉善右旗| 衢州市| 徐汇区| 丹寨县| 五大连池市| 梧州市| 浑源县| 临邑县| 隆化县| 沙坪坝区| 石林| 长寿区| 泾阳县| 乌兰浩特市| 海南省| 澄江县| 古浪县| 隆林| 奈曼旗| 丰城市| 万安县| 高州市| 五大连池市| 云安县| 三门县| 和硕县| 烟台市| 霍林郭勒市|