字符串排序

题目: 字符串排序

思路: 1. 输入一个字符串,

         2. 将字符串转换为一个字符数组

         3. 对字符数组进行排序

         4. 打印结果

public class 第四十题字符串排序 {
    public static void main(String[] args) {
        System.out.print("请输入一行字符串:");
        Scanner in = new Scanner(System.in);
        String s = in.nextLine();
        System.out.print("排序后的字符串为:");
        for(char c:sort(s)) {
            System.out.print(c +"");
        }
        in.close();
        
    }
    //排序字符串的函数
    public static char[] sort(String s) {
        //定义一个数组装字符串
        char[] c = s.toCharArray();
        //使用冒泡排序对对字符数组进行排序
        int i=0;
        int count = 0;
        while(true) {
            if(i==c.length-1) {
                if(count == c.length -1) { //代表排序完成
                    break;
                }
                i = -1;
                count = 0;
            } else if(c[i] > c[i+1]) {
                 char temp = c[i];
                 c[i] = c[i+1];
                 c[i+1] = temp;
            } else {
                count++;
            }
            i++;
        }
        return c;
    }
}
原文地址:https://www.cnblogs.com/zjulanjian/p/10952735.html