From ab091d839e8dda02352c25373f9b12408c51fde0 Mon Sep 17 00:00:00 2001 From: ayushkumar Date: Mon, 7 Aug 2023 09:08:00 +0530 Subject: [PATCH 1/2] Bubble Sorting --- src/main/java/org/example/ListExamples.java | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/example/ListExamples.java b/src/main/java/org/example/ListExamples.java index 978cea4..0fc4124 100644 --- a/src/main/java/org/example/ListExamples.java +++ b/src/main/java/org/example/ListExamples.java @@ -28,7 +28,7 @@ public static void main(String[] args) { // min(nums); // secondLargest(nums); - + System.out.println(bubbleSorting(nums)); } @@ -210,7 +210,19 @@ else if (num / ((values[k]) * 4) == 1) { } return bucket; } - + public static List bubbleSorting(List list) { + int length = list.size(); + while (length > -1) { + for (int i = 0; i < length - 1; i++) { + if (list.get(i) > list.get(i + 1)) { + list.add(i, list.get(i + 1)); + list.remove(i + 2); + } + } + length = length - 1; + } + return list; + } } From fd3ba0d9ebcfb403809ee37d7c1758bd4c07840b Mon Sep 17 00:00:00 2001 From: ayushkumar Date: Mon, 7 Aug 2023 15:53:35 +0530 Subject: [PATCH 2/2] Bubble Sorting Using Array --- src/main/java/org/example/ListExamples.java | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/org/example/ListExamples.java b/src/main/java/org/example/ListExamples.java index 0fc4124..08c5af6 100644 --- a/src/main/java/org/example/ListExamples.java +++ b/src/main/java/org/example/ListExamples.java @@ -28,7 +28,7 @@ public static void main(String[] args) { // min(nums); // secondLargest(nums); - System.out.println(bubbleSorting(nums)); + System.out.println(bubbleSorting(num)); } @@ -210,18 +210,20 @@ else if (num / ((values[k]) * 4) == 1) { } return bucket; } - public static List bubbleSorting(List list) { - int length = list.size(); - while (length > -1) { + public static int[] bubbleSorting(int[] array) { + int length = array.length; + int buffer = 0; + while (length > 0) { for (int i = 0; i < length - 1; i++) { - if (list.get(i) > list.get(i + 1)) { - list.add(i, list.get(i + 1)); - list.remove(i + 2); + if (array[i] > array[i + 1]) { + buffer = array[i]; + array[i] = array[i + 1]; + array[i + 1] = buffer; } } length = length - 1; } - return list; + return array; }