您的位置:首页 > 其它

冒泡排序,排序字符

2014-01-18 14:19 197 查看
package com.order.test;

public class Order {

public static void main(String[] args) {

String [] datas=new String[]{"75","新浪","70","中华人民","90","95","85","80","X","L","XL","XML","12","A","B","D","Z","Y","X","110","150","140","130","99"};
for (int i = 0; i < datas.length -1; i++){    //最多做n-1趟排序
for(int j = 0 ;j < datas.length - i - 1; j++){    //对当前无序区间score[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
String value1=datas[j];
String value2=datas[j+1];
if(checkNumber(value1)&&checkNumber(value2)){
if(Integer.parseInt(value1)>Integer.parseInt(value2)){
String temp = datas[j];
datas[j] = datas[j + 1];
datas[j + 1] = temp;
}
}else if(checkNumber(value2)&&!checkNumber(value1)){
String temp = datas[j];
datas[j] = datas[j+1];
datas[j+1] = temp;
}
}
}
System.out.print("最终排序结果:");
for(int a = 0; a < datas.length; a++){
System.out.print(datas[a] + " ");
}
}
private static boolean checkNumber(String key){
for(int i=0;i<key.length();i++){
char ch=key.charAt(i);
if(ch<'0'||ch>'9'){
return false;
}
}

return true;
}

}

结果:

最终排序结果:12 70 75 80 85 90 95 99 110 130 140 150 新浪 中华人民 X L XL XML A B D Z Y X
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  冒泡排序