From db5a5e778a4e7f5b3559c3b6e7ef1359255de6e0 Mon Sep 17 00:00:00 2001 From: gizee <1344584929> Date: Sun, 20 Oct 2024 06:54:53 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8E=92=E5=BA=8F=E4=BB=A3?= =?UTF-8?q?=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- codes/openZnolis/18398010.java | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 codes/openZnolis/18398010.java diff --git a/codes/openZnolis/18398010.java b/codes/openZnolis/18398010.java new file mode 100644 index 000000000..d78880e1b --- /dev/null +++ b/codes/openZnolis/18398010.java @@ -0,0 +1,24 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int [] a, int n){ + // 你的代码,使无序数组 a 变得有序 + boolean swapped; + for (int i = 0; i < n - 1; i++) { + swapped = false; + for (int j = 0; j < n - 1 - i; j++) { + if (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