题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
冒泡排序
import java.io.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
String str = "";
while ((str = reader.readLine()) != null) {
char[] arr = str.toCharArray();
int start = 0, end = arr.length - 1;
for (int i = 0;; i++) {
if ( !(arr[i] >= 'A' && arr[i] <= 'Z') &&
!(arr[i] >= 'a' && arr[i] <= 'z')) continue;//跳过开头的非字母
start = i;
break;
}
for (int i = end;; i--) {
if ( !(arr[i] >= 'A' && arr[i] <= 'Z') &&
!(arr[i] >= 'a' && arr[i] <= 'z')) continue;//跳过尾部的非字母
end = i;
break;
}
for (int i = start; i < end; i++) {
for (int j = start; j < end - (i - start); j++) {
char max = arr[j];
char compA = max;//保存小写,用来比较
if (max >= 'A' && max <= 'Z') {
compA += 'a' - 'A';
}
int k = j + 1;//新建一个指针寻找要比较的字母
while (k <= end - (i - start) && !(arr[k] >= 'A' && arr[k] <= 'Z') &&
!(arr[k] >= 'a' && arr[k] <= 'z')) k++;
if (k > end - (i - start)) break;
char compB = arr[k];
if (arr[k] >= 'A' && arr[k] <= 'Z') {
compB += 'a' - 'A';
}
if (compB < compA) {
arr[j] = arr[k];
arr[k] = max;
}
j = k - 1;//移动指针到下次交换的位置前面
}
}
System.out.println(arr);
}
}
}
