From 6619a819c6d99ceab7fc9bb7c8ccf27c76910cb8 Mon Sep 17 00:00:00 2001 From: zhang-kang1344319 <941975659@qq.com> Date: Tue, 19 Nov 2024 19:43:26 +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/abfbak/9622480.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 codes/abfbak/9622480.java diff --git a/codes/abfbak/9622480.java b/codes/abfbak/9622480.java new file mode 100644 index 000000000..cf255de2f --- /dev/null +++ b/codes/abfbak/9622480.java @@ -0,0 +1,28 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int[] a, int n) { + if (a == null || n <= 1) { + return; // 如果数组为空或只有一个元素,无需排序 + } + for (int i = 0; i < n - 1; i++) { + boolean swapped = false; // 标记当前轮是否有交换 + for (int j = 0; j < n - 1 - i; 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; + swapped = true; // 标记交换发生 + } + } + // 如果没有发生交换,说明数组已经有序 + if (!swapped) { + break; + } + } +} //end + -- Gitee From dd4d2c6386fa4995d9c54d2e5ba250b3fe743dda Mon Sep 17 00:00:00 2001 From: zhang-kang1344319 <941975659@qq.com> Date: Tue, 19 Nov 2024 19:49:29 +0800 Subject: [PATCH 2/2] test --- codes/adfbak/9622480.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 codes/adfbak/9622480.java diff --git a/codes/adfbak/9622480.java b/codes/adfbak/9622480.java new file mode 100644 index 000000000..cf255de2f --- /dev/null +++ b/codes/adfbak/9622480.java @@ -0,0 +1,28 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int[] a, int n) { + if (a == null || n <= 1) { + return; // 如果数组为空或只有一个元素,无需排序 + } + for (int i = 0; i < n - 1; i++) { + boolean swapped = false; // 标记当前轮是否有交换 + for (int j = 0; j < n - 1 - i; 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; + swapped = true; // 标记交换发生 + } + } + // 如果没有发生交换,说明数组已经有序 + if (!swapped) { + break; + } + } +} //end + -- Gitee