2013-07-10 36 views
-1

用空格分隔字符串中的元素怎麼能sorted.I分離子有以下字符串:如何排序由空格字符串中的

temp = abcd bcda gfre dfgre fwft efwe 

    //temp.size() gives 30 
    //after the sort temp should look like ie temp = abcd bcda dfgre efwe fwft gfre 

我需要在TEMP中元素進行排序儘可能少的時間。請注意,我正在處理的溫度的大小是10的權力7我忘了提及,我已經嘗試過的Collections.sort和Array.sort,它們比需要的時間要多得多。我需要的是比這更快的算法嗎?

+5

好吧,你嘗試過什麼? (這裏有兩個或三個任務 - 拆分,排序和可能的加入...) –

+0

將字符串拆分爲單詞,對單詞數組進行排序。 –

+0

你對你想使用的排序算法有偏好嗎? – Brian

回答

1
String [] array = temp.split("\\s+"); // split by whitespace 
Arrays.sort(array); // sort using mergesort with insertionsort 

StringBuilder sb = new StringBuilder(temp.length()); 

for(String s : array){ 
    sb.append(s).append(" "); 
} 

temp = sb.toString(); // assign temp the new string 
2
  1. 使用分割使用集合Arrays.sort().split(seperator)
  2. 排序字符串