日期:2014-05-19 浏览次数:20868 次
public static String bubbleSort(String str){ char[] chars = new char[str.length()]; for(int i = 0 ; i < str.length() ; i++){ chars[i] = str.charAt(i); } for(int i = 0 ; i < chars.length ; i++){ for(int j = i + 1 ; j < chars.length ; j ++){ char temp; if(chars[i] > chars[j]){ temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } } } String retStr = ""; for(int i = 0 ; i < chars.length ; i ++){ retStr += chars[i]; } return retStr; }
------解决方案--------------------
LZ这个是完整版的
public class BubbleSortTest { public static String bubbleSort(String str){ char[] chars = new char[str.length()]; for(int i = 0 ; i < str.length() ; i++){ chars[i] = str.charAt(i); } for(int i = 0 ; i < chars.length ; i++){ for(int j = i + 1 ; j < chars.length ; j ++){ char temp; if(chars[i] > chars[j]){ temp = chars[i]; chars[i] = chars[j]; chars[j] = temp; } } } String retStr = ""; for(int i = 0 ; i < chars.length ; i ++){ retStr += chars[i]; } return retStr; } public static void main(String[] args) { System.out.println(bubbleSort(args.length > 0 ? args[0] : "kief351"));//如果控制端没有输入字符串就默认传一个 } }
------解决方案--------------------