public class BubbleSort { public static void main(String[] args) { int[] array = {5, 3, 8, 2, 1, 4}; System.out.println(原始数组:; printArray(array); bubbleSort(array); System.out.println(排序后的数组:"); printArray(array); } public static void bubbleSort(int[] array) { int n = array.length; for (int i = 0; i < n - 1; i++) { for (int j = 0; j < n - i - 1; j++) { if (array[j] > array[j + 1]) { // 交换相邻元素的位置 int temp = array[j]; array[j] = array[j + 1]; array[j + 1] = temp; } } } } public static void printArray(int[] array) { for (int i = 0; i < array.length; i++) { System.out.print(array[i] + " "); } System.out.println(); }}