diff --git a/codes/guzy/IK2IEV.java b/codes/guzy/IK2IEV.java new file mode 100644 index 0000000000000000000000000000000000000000..c7fcc0c92737686c2c5a2e46f84403a126b033b1 --- /dev/null +++ b/codes/guzy/IK2IEV.java @@ -0,0 +1,30 @@ +public class IK2IEV { + public static void bubbleSort(int[] arr) { + if (arr == null || arr.length <= 1) { + return; + } + int len = arr.length; + for (int i = 0; i < len - 1; i++) { + boolean swapFlag = false; + for (int j = 0; j < len - 1 - i; j++) { + if (arr[j] > arr[j+1]) { + int temp = arr[j]; + arr[j] = arr[j+1]; + arr[j+1] = temp; + swapFlag = true; + } + } + if (!swapFlag) break; + } + } + public static void main(String[] args) { + int[] testArr = {6, 2, 9, 1, 5, 3}; + System.out.println("排序前:"); + for(int num : testArr) System.out.print(num + " "); + + bubbleSort(testArr); + + System.out.println("排序后:"); + for(int num : testArr) System.out.print(num + " "); + } +} \ No newline at end of file