From 19d21b965749abd558259f58db72236ad560e24b Mon Sep 17 00:00:00 2001 From: coffe153 <2799451587@qq.com> Date: Mon, 4 Nov 2024 16:01:58 +0800 Subject: [PATCH] public static void bubbleSort(int[] a) { --- codes/d2799451587/18561048.java | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 codes/d2799451587/18561048.java diff --git a/codes/d2799451587/18561048.java b/codes/d2799451587/18561048.java new file mode 100644 index 00000000..5cc795c4 --- /dev/null +++ b/codes/d2799451587/18561048.java @@ -0,0 +1,30 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ +public static void bubbleSort(int [] a){ + // 你的代码,使无序数组 a 变得有序 + int n = a.length; + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (a[j] > a[j + 1]) { + // 交换元素 + int temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + } + } + } + } + + public static void main(String[] args) { + int[] a = {3,9,13,28,34,49,57}; + bubbleSort(a); + for (int num : a) { + System.out.print(num + " "); + } + } + +} //end -- Gitee