题解 | #字符串排序#
字符串排序
https://www.nowcoder.com/practice/5190a1db6f4f4ddb92fd9c365c944584
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); String sentence = in.nextLine(); char[] charArr = sentence.toCharArray(); bubbleSort(charArr); for (char c : charArr) { System.out.print(c); } } private static void bubbleSort(char[] arr) { for (int i = 0; i < arr.length; i++) { int j = 0; int k = 1; while (k < arr.length - i) { if (!Character.isLetter(arr[j])) { j = k; k = j + 1; continue; } if (!Character.isLetter(arr[k])) { k++; continue; } if (Character.toLowerCase(arr[j]) > Character.toLowerCase(arr[k])) { char tmp = arr[j]; arr[j] = arr[k]; arr[k] = tmp; } j = k; k = j + 1; } } } }