From d0ea964b2d10669f30cff9afa1dd7acd7ae3ceaf Mon Sep 17 00:00:00 2001 From: danqing <3237707794@qq.com@qq.com> Date: Tue, 28 May 2024 16:18:55 +0800 Subject: [PATCH] =?UTF-8?q?danqing=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 --- DQdanqing/16462521.java | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 DQdanqing/16462521.java diff --git a/DQdanqing/16462521.java b/DQdanqing/16462521.java new file mode 100644 index 00000000..0f659cbc --- /dev/null +++ b/DQdanqing/16462521.java @@ -0,0 +1,28 @@ +public class BubbleSort { + public static void main(String[] args) { + int[] arr = {64, 34, 25, 12, 22, 11, 90}; + bubbleSort(arr); + System.out.println("Sorted array"); + printArray(arr); + } + + // 冒泡排序函数 + static void bubbleSort(int arr[]) { + int n = arr.length; + for (int i = 0; i < n-1; i++) + for (int j = 0; j < n-i-1; j++) + if (arr[j] > arr[j+1]) { + // 交换 arr[j+1] 和 arr[j] + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + } + } + + // 打印数组的函数 + static void printArray(int arr[]) { + for (int i=0; i < arr.length; i++) + System.out.print(arr[i] + " "); + System.out.println(); + } +} -- Gitee