From befd8413d704c0205dfa2734eeed30142c2f56ae Mon Sep 17 00:00:00 2001 From: Sun_gh_pku Date: Wed, 25 Sep 2024 12:55:05 +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/Sun_gh_pku/18171390.java | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 codes/Sun_gh_pku/18171390.java diff --git a/codes/Sun_gh_pku/18171390.java b/codes/Sun_gh_pku/18171390.java new file mode 100644 index 00000000..72f57b31 --- /dev/null +++ b/codes/Sun_gh_pku/18171390.java @@ -0,0 +1,23 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int [] a, int n){ + // 你的代码,使无序数组 a 变得有序 + // 外层循环控制排序的趟数 + for (int i = 0; i < n - 1; i++) { + // 内层循环进行相邻元素的比较和交换 + 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; + } + } + } + return a; // 返回排序后的数组 + +} //end -- Gitee From b1e5b8422d4b712e944df11b2a75e4b6917475e8 Mon Sep 17 00:00:00 2001 From: Sun_gh_pku Date: Wed, 25 Sep 2024 13:03:27 +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/Sun_gh_pku/18171390.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/codes/Sun_gh_pku/18171390.java b/codes/Sun_gh_pku/18171390.java index 72f57b31..421e92a6 100644 --- a/codes/Sun_gh_pku/18171390.java +++ b/codes/Sun_gh_pku/18171390.java @@ -6,18 +6,14 @@ */ public static void bubbleSort(int [] a, int n){ // 你的代码,使无序数组 a 变得有序 - // 外层循环控制排序的趟数 for (int i = 0; i < n - 1; i++) { - // 内层循环进行相邻元素的比较和交换 for (int j = 0; j < n - 1 - i; j++) { if (a[j] > a[j + 1]) { - // 交换 a[j] 和 a[j + 1] + // 交换 arr[j] 和 arr[j + 1] int temp = a[j]; a[j] = a[j + 1]; a[j + 1] = temp; } } } - return a; // 返回排序后的数组 - } //end -- Gitee