diff --git a/codes/Yang04/18909633.java b/codes/Yang04/18909633.java new file mode 100644 index 0000000000000000000000000000000000000000..3df71a24a926c33ee20f08e072cdfaa2d8ee66ab --- /dev/null +++ b/codes/Yang04/18909633.java @@ -0,0 +1,18 @@ +public static void bubbleSort(int[] a, int n) { + if (a == null || n <= 1) return; + + for (int i = 0; i < n - 1; i++) { + boolean swapped = false; + + for (int j = 0; j < n - 1 - i; j++) { + if (a[j] > a[j + 1]) { + int temp = a[j]; + a[j] = a[j + 1]; + a[j + 1] = temp; + swapped = true; + } + } + + if (!swapped) break; + } + }