From d4fe23cfbdd3ad3e5530f3276f8dbe3e53aba622 Mon Sep 17 00:00:00 2001 From: Amyaaa <1558539974@qq.com> Date: Wed, 3 Apr 2024 16:56:01 +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/Amyaaa/15769404.java | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 codes/Amyaaa/15769404.java diff --git a/codes/Amyaaa/15769404.java b/codes/Amyaaa/15769404.java new file mode 100644 index 000000000..764849b98 --- /dev/null +++ b/codes/Amyaaa/15769404.java @@ -0,0 +1,22 @@ +public class BubbleSort { + public static void bubbleSort(int[] arr) { + int n = arr.length; + for (int i = 0; i < n; i++) { + // 每轮遍历将最大的数移到末尾 + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j+1]) { + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + } + } + } + } + + public static void main(String[] args) { + int[] arr = {64, 34, 25, 12, 22, 11, 90}; + bubbleSort(arr); + System.out.println(Arrays.toString(arr)); // [11, 12, 22, 25, 34, 64, 90] + } +} + -- Gitee