|
| 1 | +package main.code.array.sort; |
| 2 | + |
| 3 | +import java.util.Arrays; |
| 4 | + |
| 5 | +public class Code_00_BubbleSort { |
| 6 | + |
| 7 | + public static void bubbleSort(int[] arr) { |
| 8 | + if (arr == null || arr.length < 2) { |
| 9 | + return; |
| 10 | + } |
| 11 | + for (int e = arr.length - 1; e > 0; e--) { |
| 12 | + for (int i = 0; i < e; i++) { |
| 13 | + if (arr[i] > arr[i + 1]) { |
| 14 | + swap(arr, i, i + 1); |
| 15 | + } |
| 16 | + } |
| 17 | + } |
| 18 | + } |
| 19 | + |
| 20 | + public static void swap(int[] arr, int i, int j) { |
| 21 | + arr[i] = arr[i] ^ arr[j]; |
| 22 | + arr[j] = arr[i] ^ arr[j]; |
| 23 | + arr[i] = arr[i] ^ arr[j]; |
| 24 | + } |
| 25 | + |
| 26 | + // for test |
| 27 | + public static void comparator(int[] arr) { |
| 28 | + Arrays.sort(arr); |
| 29 | + } |
| 30 | + |
| 31 | + // for test |
| 32 | + public static int[] generateRandomArray(int maxSize, int maxValue) { |
| 33 | + int[] arr = new int[(int) ((maxSize + 1) * Math.random())]; |
| 34 | + for (int i = 0; i < arr.length; i++) { |
| 35 | + arr[i] = (int) ((maxValue + 1) * Math.random()) - (int) (maxValue * Math.random()); |
| 36 | + } |
| 37 | + return arr; |
| 38 | + } |
| 39 | + |
| 40 | + // for test |
| 41 | + public static int[] copyArray(int[] arr) { |
| 42 | + if (arr == null) { |
| 43 | + return null; |
| 44 | + } |
| 45 | + int[] res = new int[arr.length]; |
| 46 | + for (int i = 0; i < arr.length; i++) { |
| 47 | + res[i] = arr[i]; |
| 48 | + } |
| 49 | + return res; |
| 50 | + } |
| 51 | + |
| 52 | + // for test |
| 53 | + public static boolean isEqual(int[] arr1, int[] arr2) { |
| 54 | + if ((arr1 == null && arr2 != null) || (arr1 != null && arr2 == null)) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + if (arr1 == null && arr2 == null) { |
| 58 | + return true; |
| 59 | + } |
| 60 | + if (arr1.length != arr2.length) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + for (int i = 0; i < arr1.length; i++) { |
| 64 | + if (arr1[i] != arr2[i]) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + return true; |
| 69 | + } |
| 70 | + |
| 71 | + // for test |
| 72 | + public static void printArray(int[] arr) { |
| 73 | + if (arr == null) { |
| 74 | + return; |
| 75 | + } |
| 76 | + for (int i = 0; i < arr.length; i++) { |
| 77 | + System.out.print(arr[i] + " "); |
| 78 | + } |
| 79 | + System.out.println(); |
| 80 | + } |
| 81 | + |
| 82 | + // for test |
| 83 | + public static void main(String[] args) { |
| 84 | + int testTime = 500000; |
| 85 | + int maxSize = 100; |
| 86 | + int maxValue = 100; |
| 87 | + boolean succeed = true; |
| 88 | + for (int i = 0; i < testTime; i++) { |
| 89 | + int[] arr1 = generateRandomArray(maxSize, maxValue); |
| 90 | + int[] arr2 = copyArray(arr1); |
| 91 | + bubbleSort(arr1); |
| 92 | + comparator(arr2); |
| 93 | + if (!isEqual(arr1, arr2)) { |
| 94 | + succeed = false; |
| 95 | + break; |
| 96 | + } |
| 97 | + } |
| 98 | + System.out.println(succeed ? "Nice!" : "Fucking fucked!"); |
| 99 | + |
| 100 | + int[] arr = generateRandomArray(maxSize, maxValue); |
| 101 | + printArray(arr); |
| 102 | + bubbleSort(arr); |
| 103 | + printArray(arr); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments