日期:2014-05-20 浏览次数:20812 次
import java.util.Scanner; public class Sort { public static void main(String[] args) { Scanner sca = new Scanner(System.in); System.out.println("请输入一个字符串:"); String s = sca.next(); System.out.println("排序前的字符串为: " + s + "\n" + "排序后的字符串为: " + sort1(s)); } public static String sort1(String s){ //插入排序 char[] array = s.toCharArray(); for(int i = 1;i < s.length();i++){ char temp = array[i]; int j = i - 1; //大的字符后移操作 while(array[j] > temp && j >= 0){ array[j + 1] = array[j]; j--; } //插入操作 array[j + 1] = temp; } StringBuffer ss = new StringBuffer(); ss.append(array); return ss.toString(); } }