diff --git a/codes/wushusong/21667339.java b/codes/wushusong/21667339.java new file mode 100644 index 0000000000000000000000000000000000000000..1a87a3e7ed4a804d508c96a478c2c2410394be47 --- /dev/null +++ b/codes/wushusong/21667339.java @@ -0,0 +1,22 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int[] a, int n) { + // 你的代码,使无序数组 a 变得有序 + // 外层循环控制排序轮数 + for (int i = 0; i < n - 1; i++) { + // 内层循环进行相邻元素比较和交换 + for (int j = 0; j < n - i - 1; j++) { + // 如果前面的元素大于后面的元素,则交换它们 + if (a[j] > a[j + 1]) { + // 交换 arr[j] 和 arr[j+1] + int temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } +} //end \ No newline at end of file