采用冒泡排序对如下数组排序 String[] arr = { "Now", "is", "the", "time", "for","all", "good", "men", "to", "come", "to", "the", "aid", "of", "their", "country" };

解决方案 »

  1.   

    public class BunbleSort { public static void main(String[] args) {
    // TODO Auto-generated method stub
    String[] arr = { "Now", "is", "the", "time", "for", "all", "good",
    "men", "to", "come", "to", "the", "aid", "of", "their",
    "country" };
    print(BunbleSort(arr));
    } // 冒泡排序
    public static String[] BunbleSort(String[] data) {
    for (int i = 0; i < data.length; i++) {
    for (int j = 0; j < data.length - i - 1; j++) {
    if (data[j].compareToIgnoreCase(data[j + 1]) > 0) {
    String temp = data[j];
    data[j] = data[j + 1];
    data[j + 1] = temp;
    }
    } }
    return data;
    } // 打印数组
    public static void print(String[] data) {
    for (String s : data)
    System.out.print(s + " ");
    System.out.println();
    }
    }
      

  2.   

    输出结果:aid all come country for good is men Now of the the their time to to