package org.zsl.algorithm.sort.bubble; public class _1 { public static void main(String[] args) { String[] a = { "Alice", "Bull", "Decjsk", "Cal", "Zol", "Jack", "Tom", "Lily" }; sort(a); show(a); } private static void show(Comparable[] a) { for (int i = 0; i < a.length; i++) { System.out.print(a[i] + "、"); } System.out.println(); } public static void sort(Comparable[] a) { for (int i = 0; i < a.length; i++) {// 仅仅只是用来控制循环次数 for (int j = 0; j < a.length - 1; j++) {// 用来进行比较 if (a[j].compareTo(a[j + 1]) > 0) { swap(a, j, j + 1); } } } } private static void swap(Comparable[] a, int i, int j) { Comparable temp = a[i]; a[i] = a[j]; a[j] = temp; } public static boolean isSorted(Comparable[] a) { for (int i = 0; i < a.length - 1; i++) { if (a[i].compareTo(a[i + 1]) > 0) // 判断是否有没有排序的元素 return false; } return true; } }