package firstExam.ninth;
/**
*已知一组字符串String[] strs={“kkk”,”abc”,”dcb”,”okx”}
*请使用一种算法,对字符串进行升序排列
*/
public class Test {
public static void main(String[] args) {
String[] strs={"kkk","abc","dcb","okx","aab"};
System.out.println("原数组是:");
for(int i=0;i<strs.length;i++){
System.out.print(strs[i]+"\t");
}
String[] sortStrs=sortString(strs);
System.out.println("\n排序后的数组是:");
for(int i=0;i<sortStrs.length;i++){
System.out.print(sortStrs[i]+"\t");
}
}
public static String[] sortString(String[] strs){
String beforeStr=null;
for(int i=0;i<strs.length;i++){
beforeStr=strs[i];
int j;
int index=i;
for(j=i+1;j<strs.length;j++){
if(beforeStr.compareToIgnoreCase(strs[j])>0){
beforeStr=strs[j];
index=j;
}
}
strs[index]=strs[i];
strs[i]=beforeStr;
}
return strs;
}
}
package firstExam.ninth;
public class TeaStringSort {
public static void main(String[] args) {
String[] strs = {"kkk","abc","dcb","acb","okx"};
strs=maopao(strs);
for(String string : strs){
System.out.println(string);
}
}
public static String[] maopao(String[] strings){
for(int i=strings.length-1;i>=0;i--){
for(int j=0;j<i;j++){
if(strings[j].compareTo(strings[j+1])>0){
String temp = strings[j];
strings[j] = strings[j+1];
strings[j+1]=temp;
}
}
}
return strings;
}
}