首页 > 其他 > 详细

冒泡排序,排序字符

时间:2014-01-20 23:03:50      阅读:401      评论:0      收藏:0      [点我收藏+]
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

冒泡排序,排序字符

原文:http://blog.csdn.net/dreamerframework/article/details/18449621

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!