用JAVA写个冒泡排序

public class BubbleSort {

// 基础版本:整数数组排序

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 main(String[] args) {
    // 测试整数排序
    int[] numbers = {64, 34, 25, 12, 22, 11, 90};
    System.out.println("排序前:" + Arrays.toString(numbers));
    bubbleSort(numbers);
    System.out.println("排序后:" + Arrays.toString(numbers));
}

}

更新 2025年5月7日