From 211c28bb7ba7e169eb8b2b2b9a5fd745cba2b25f Mon Sep 17 00:00:00 2001 From: sainath Date: Fri, 1 Aug 2025 15:01:11 +0530 Subject: [PATCH 1/2] Add BubbleSort algorithm --- .../com/thealgorithms/sorting/BubbleSort.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 src/main/java/com/thealgorithms/sorting/BubbleSort.java 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..b81c0360d7ff --- /dev/null +++ b/src/main/java/com/thealgorithms/sorting/BubbleSort.java @@ -0,0 +1,16 @@ +package com.thealgorithms.sorting; + +public class BubbleSort { + public static void bubbleSort(int[] arr) { + int n = arr.length; + for (int i = 0; i < n - 1; i++) { + for (int j = 0; j < n - i - 1; j++) { + if (arr[j] > arr[j + 1]) { + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } +} From 28eafded6a7dec69425fcadc70db34365ae0df88 Mon Sep 17 00:00:00 2001 From: sainath Date: Fri, 1 Aug 2025 15:28:57 +0530 Subject: [PATCH 2/2] Add BubbleSort algorithm --- src/main/java/com/thealgorithms/sorting/BubbleSort.java | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/thealgorithms/sorting/BubbleSort.java b/src/main/java/com/thealgorithms/sorting/BubbleSort.java index b81c0360d7ff..c1d82bf3a6ba 100644 --- a/src/main/java/com/thealgorithms/sorting/BubbleSort.java +++ b/src/main/java/com/thealgorithms/sorting/BubbleSort.java @@ -1,16 +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++) { - for (int j = 0; j < n - i - 1; j++) { + 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; } } } pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy