From ba5d7032ea09f05273603ea084505fa62e5882ed Mon Sep 17 00:00:00 2001 From: lvwentao Date: Sat, 8 Feb 2025 10:19:48 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/w18515601045/19446136.java | 40 ++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 codes/w18515601045/19446136.java diff --git a/codes/w18515601045/19446136.java b/codes/w18515601045/19446136.java new file mode 100644 index 000000000..4381873b2 --- /dev/null +++ b/codes/w18515601045/19446136.java @@ -0,0 +1,40 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ + +public class BubbleSort { + public static void bubbleSort(int[] a, int n) { + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (a[j] > a[j + 1]) { + // 交换 a[j] 和 a[j + 1] + int temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } + } + + public static void main(String[] args) { + int[] a = {34, 7, 23, 32, 5, 62}; + int n = a.length; + System.out.println("排序前的数组: "); + for (int num : a) { + System.out.print(num + " "); + } + System.out.println(); + + // 调用冒泡排序方法 + bubbleSort(a, n); + + System.out.println("排序后的数组: "); + for (int num : a) { + System.out.print(num + " "); + } + } +} + -- Gitee From 60b65aa83050d43f2909a267a14d5229913fea82 Mon Sep 17 00:00:00 2001 From: lvwentao Date: Sat, 8 Feb 2025 10:47:55 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=92=E5=BA=8F?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/w18515601045/19446136.java | 33 ++++++++++++++++++++------------ 1 file changed, 21 insertions(+), 12 deletions(-) diff --git a/codes/w18515601045/19446136.java b/codes/w18515601045/19446136.java index 4381873b2..0b30956fc 100644 --- a/codes/w18515601045/19446136.java +++ b/codes/w18515601045/19446136.java @@ -1,28 +1,38 @@ -/** - * 冒泡排序函数 - * aa bb cc - * @param a 待排序的数组 - * @param n 待排序的数组长度 - */ +public class BubbleSortExample { -public class BubbleSort { + /** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ public static void bubbleSort(int[] a, int n) { + // 外层循环控制排序的轮数,总共需要进行 n - 1 轮 for (int i = 0; i < n - 1; i++) { + // 标记本轮是否发生了交换,若未发生交换说明数组已经有序 + boolean swapped = false; + // 内层循环进行相邻元素的比较和交换 for (int j = 0; j < n - i - 1; j++) { + // 如果当前元素比下一个元素大,则交换它们的位置 if (a[j] > a[j + 1]) { - // 交换 a[j] 和 a[j + 1] int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; + // 发生了交换,将标记置为 true + swapped = true; } } + // 如果本轮没有发生交换,说明数组已经有序,提前结束排序 + if (!swapped) { + break; + } } } public static void main(String[] args) { - int[] a = {34, 7, 23, 32, 5, 62}; + int[] a = {5, 3, 8, 4, 2}; int n = a.length; - System.out.println("排序前的数组: "); + System.out.println("排序前的数组:"); for (int num : a) { System.out.print(num + " "); } @@ -31,10 +41,9 @@ public class BubbleSort { // 调用冒泡排序方法 bubbleSort(a, n); - System.out.println("排序后的数组: "); + System.out.println("排序后的数组:"); for (int num : a) { System.out.print(num + " "); } } } - -- Gitee