From 0d562de1760d825a5b094a5db61bda7912a24c74 Mon Sep 17 00:00:00 2001 From: Shephatiah <412349939@qq.com> Date: Mon, 25 Nov 2024 14:39:17 +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/Shephatiah/18693174.java | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 codes/Shephatiah/18693174.java diff --git a/codes/Shephatiah/18693174.java b/codes/Shephatiah/18693174.java new file mode 100644 index 000000000..856d733ff --- /dev/null +++ b/codes/Shephatiah/18693174.java @@ -0,0 +1,25 @@ +public class BubbleSort { + public static void bubbleSort(int[] arr) { + int n = arr.length; + // 一共进行n - 1轮排序 + for (int i = 0; i < n - 1; i++) { + // 每一轮比较的次数会逐渐减少 + for (int j = 0; j < n - 1 - i; 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 = {5, 4, 3, 2, 1}; + bubbleSort(arr); + for (int num : arr) { + System.out.print(num + " "); + } + } +} -- Gitee