From 025042da3f280d0637742d9cff19d737e5cb4cb2 Mon Sep 17 00:00:00 2001 From: wushusong Date: Thu, 4 Sep 2025 10:50:26 +0000 Subject: [PATCH 1/2] =?UTF-8?q?add=20codes/wushusong/21667339.java.=20?= =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=86=92=E6=B3=A1=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wushusong --- codes/wushusong/21667339.java | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 codes/wushusong/21667339.java diff --git a/codes/wushusong/21667339.java b/codes/wushusong/21667339.java new file mode 100644 index 000000000..d3b0f540f --- /dev/null +++ b/codes/wushusong/21667339.java @@ -0,0 +1,20 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int [] a, int n){ + // 你的代码,使无序数组 a 变得有序 + for(int i = 0; i < n; i++) { + for(int j = 0; j < n - i; j++) { + if(a[j - 1] > a[j]) { + int temp = a[j - 1]; + a [j - 1] = a[j]; + a[j] = temp; + } + } + } + + +} //end \ No newline at end of file -- Gitee From e390163026775bb4499ebf91a85c35231860ac33 Mon Sep 17 00:00:00 2001 From: wushusong Date: Thu, 4 Sep 2025 10:53:22 +0000 Subject: [PATCH 2/2] =?UTF-8?q?update=20codes/wushusong/21667339.java.=20?= =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=86=92=E6=B3=A1=E6=8E=92=E5=BA=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wushusong --- codes/wushusong/21667339.java | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/codes/wushusong/21667339.java b/codes/wushusong/21667339.java index d3b0f540f..1a87a3e7e 100644 --- a/codes/wushusong/21667339.java +++ b/codes/wushusong/21667339.java @@ -4,17 +4,19 @@ * @param a 待排序的数组 * @param n 待排序的数组长度 */ -public static void bubbleSort(int [] a, int n){ +public static void bubbleSort(int[] a, int n) { // 你的代码,使无序数组 a 变得有序 - for(int i = 0; i < n; i++) { - for(int j = 0; j < n - i; j++) { - if(a[j - 1] > a[j]) { - int temp = a[j - 1]; - a [j - 1] = a[j]; - a[j] = temp; - } - } + // 外层循环控制排序轮数 + 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 -- Gitee