From 504a2bae6e3eb55ddb135b7f0cc32b6fd6cd04eb Mon Sep 17 00:00:00 2001 From: wiseman <8604402+erwin1@user.noreply.gitee.com> Date: Thu, 14 Mar 2024 07:30:15 +0000 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E5=86=92=E6=B3=A1=E6=8E=92?= =?UTF-8?q?=E5=BA=8F=E7=AE=97=E6=B3=95=E4=BB=BB=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: wiseman <8604402+erwin1@user.noreply.gitee.com> --- XUEMAO.md | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 XUEMAO.md diff --git a/XUEMAO.md b/XUEMAO.md new file mode 100644 index 00000000..f944d09a --- /dev/null +++ b/XUEMAO.md @@ -0,0 +1,30 @@ +``` +/** + * 冒泡排序函数 + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int [] a, int n){ + if (a == null || n <= 1) { + return; // 如果数组为空或只有一个元素,不需要排序 + } + 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