学习记录

排序算法 - 冒泡排序
public class BubbleSort {
public static void bubbleSort(int[] arr) {
int n = arr.length;
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (arr[j] > arr[j + 1]) {
// 交换 arr[j+1] 和 arr[j]
int temp = arr[j];
arr[j] = arr[j + 1];
arr[j + 1] = temp;
}
}
}
}

public static void main(String[] args) {
    int[] arr = {64, 34, 25, 12, 22, 11, 90};
    bubbleSort(arr);
    System.out.println("排序后的数组:");
    for (int num : arr) {
        System.out.print(num + " ");
    }
}

}

posted @ 2025-01-27 20:47  吉尼泰梅  阅读(7)  评论(0)    收藏  举报