diff --git a/src/main/java/com/thealgorithms/sorting/BubbleSort.java b/src/main/java/com/thealgorithms/sorting/BubbleSort.java new file mode 100644 index 000000000000..c1d82bf3a6ba --- /dev/null +++ b/src/main/java/com/thealgorithms/sorting/BubbleSort.java @@ -0,0 +1,21 @@ +package com.thealgorithms.sorting; +public class BubbleSort { + + public static void bubbleSort(int[] arr) { + int n = arr.length; + boolean swapped; + for (int i = 0; i < n - 1; i++) { + swapped = false; + for (int j = 0; j < n - 1 - i; j++) { + if (arr[j] > arr[j + 1]) { + // swap + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + swapped = true; + } + } + if (!swapped) break; + } + } +}
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: