From a5d4053c82b982ad85b9c5a311fae5ae10f23114 Mon Sep 17 00:00:00 2001 From: mlboy Date: Sun, 16 Mar 2025 00:17:14 +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/lostdance/19859399.java | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 codes/lostdance/19859399.java diff --git a/codes/lostdance/19859399.java b/codes/lostdance/19859399.java new file mode 100644 index 000000000..d9bd1a23a --- /dev/null +++ b/codes/lostdance/19859399.java @@ -0,0 +1,17 @@ +public static void bubbleSort(int [] a, int n){ + boolean swapped; + for (int i = 0; i < n - 1; i++) { + swapped = false; + for (int j = 0; j < n - 1 - i; j++) { + if (arr[j] > arr[j + 1]) { + // 交换元素 + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = true; + } + } + // 如果没有发生交换,说明已经有序,提前终止 + if (!swapped) break; + } +} -- Gitee