From 3afa1b6a4ddce2c58d281c9e6923ea0d1377b77f Mon Sep 17 00:00:00 2001
From: Antonynixenraj <antonynixen@gmail.com>
Date: Fri, 21 Mar 2025 16:01:37 +0530
Subject: [PATCH] bubble sort

---
 bubble_sort.java | 41 +++++++++++++++++++++++++++++++++++++++++
 1 file changed, 41 insertions(+)
 create mode 100644 bubble_sort.java

diff --git a/bubble_sort.java b/bubble_sort.java
new file mode 100644
index 0000000..7f0ed0e
--- /dev/null
+++ b/bubble_sort.java
@@ -0,0 +1,41 @@
+public class bubble_sort {
+    public static void bubbleSort(int[] arr) {
+        int n = arr.length;
+        // Traverse through all array elements
+        for (int i = 0; i < n - 1; i++) {
+            // Flag to check if swapping occurred
+            boolean swapped = false;
+
+            // Last i elements are already sorted
+            for (int j = 0; j < n - i - 1; j++) {
+                // Swap if the element is greater than the next
+                if (arr[j] > arr[j + 1]) {
+                    // Swap arr[j] and arr[j + 1]
+                    int temp = arr[j];
+                    arr[j] = arr[j + 1];
+                    arr[j + 1] = temp;
+                    swapped = true;
+                }
+            }
+
+            // If no two elements were swapped, break
+            if (!swapped) break;
+        }
+    }
+
+    public static void main(String[] args) {
+        int[] data = {64, 34, 25, 12, 22, 11, 90};
+        System.out.println("Original array:");
+        for (int num : data) {
+            System.out.print(num + " ");
+        }
+
+        // Perform bubble sort
+        bubbleSort(data);
+
+        System.out.println("\nSorted array:");
+        for (int num : data) {
+            System.out.print(num + " ");
+        }
+    }
+}
-- 
GitLab