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
诗语江南 阅读(298)
评论(0) 编辑 收藏 所属分类:
Core JAVA