1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
| import java.util.Arrays;
public class BubbleSort { public static void main(String[] args) { int[] array = new int[]{7, 1, 5, 8, 6, 2, 3, 9, 4}; int len = array.length; boolean flag = true; for (int i = 0; i < len - 1 && flag; i++) { flag = false; for (int j = i + 1; j < len; j++) { if (array[i] > array[j]) { int temp = array[i]; array[i] = array[j]; array[j] = temp; flag = true; } } } System.out.println("从小到大排序后的结果是:"); System.out.println(Arrays.toString(array));
} }
|