diff --git a/codes/xychenzhicheng/18184878.java b/codes/xychenzhicheng/18184878.java new file mode 100644 index 0000000000000000000000000000000000000000..40e7c020ceb6cce90f2fab49bf8145f3f163bccb --- /dev/null +++ b/codes/xychenzhicheng/18184878.java @@ -0,0 +1,45 @@ +/** + * 冒泡排序函数 + * aa bb cc + * @param a 待排序的数组 + * @param n 待排序的数组长度 + */ + +public class BubbleSort { + + // 冒泡排序方法 + public 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] 和 arr[j+1] + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + + // 打印数组方法 + public static void printArray(int[] arr) { + for (int i : arr) { + System.out.print(i + " "); + } + System.out.println(); + } + + public static void main(String[] args) { + int[] arr = {64, 34, 25, 12, 22, 11, 90}; + System.out.println("排序前的数组:"); + printArray(arr); + + bubbleSort(arr); + + System.out.println("排序后的数组:"); + printArray(arr); + } +} + +