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

          常用鏈接

          留言簿(2)

          隨筆分類

          隨筆檔案

          友情連接

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 汉阴县| 威海市| 沙雅县| 施秉县| 光泽县| 民丰县| 曲周县| 罗城| 安顺市| 龙里县| 姜堰市| 宁海县| 连南| 浮梁县| 新绛县| 竹山县| 铜山县| 平舆县| 阳原县| 嵊泗县| 文化| 渑池县| 茶陵县| 武山县| 泾源县| 大同县| 陈巴尔虎旗| 黄冈市| 威信县| 南部县| 达州市| 桃江县| 潮州市| 贵溪市| 南城县| 海宁市| 巍山| 玛多县| 郁南县| 高邮市| 秦安县|