From a547dd2fed98c1e51467bddf2f9dc7f528c85731 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=AA=86=E9=A9=BC=E7=AE=B1=E5=AD=90?= <753755635@qq.com> Date: Tue, 9 Apr 2024 22:47:43 +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/qq753755635/15831128.id | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 codes/qq753755635/15831128.id diff --git a/codes/qq753755635/15831128.id b/codes/qq753755635/15831128.id new file mode 100644 index 00000000..3b90719f --- /dev/null +++ b/codes/qq753755635/15831128.id @@ -0,0 +1,17 @@ +/** + * 冒泡排序函数 + * 通过相邻元素之间的比较和交换,使得每一轮比较后,最大的元素能够"浮"到数组的末尾 + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int [] a, int n){ + for(int i = 0; i < n - 1; i++){ // 外层循环,控制比较的轮数 + 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; + } + } + } +} //end -- Gitee