From 2f020d30ba253f01ec1a8788959eae24b547a9f2 Mon Sep 17 00:00:00 2001 From: Vivek Balasubramaniam Date: Sun, 24 Nov 2019 15:19:28 +0530 Subject: [PATCH 1/3] BAEL-3399: How to merge two sorted arrays into a sorted array --- .../algorithms/sortedarrays/SortedArrays.java | 34 +++++++++++++++++++ .../sortedarrays/SortedArraysUnitTest.java | 27 +++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java create mode 100644 algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java new file mode 100644 index 0000000000..286eaae243 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java @@ -0,0 +1,34 @@ +package com.baeldung.algorithms.sortedarrays; + +public class SortedArrays { + + public static int[] merge(int[] first, int[] second) { + + int m = first.length; + int n = second.length; + + int i, j, k; + i = j = k = 0; + + int[] result = new int[m + n]; + + while (i < m && j < n) { + + if (first[i] < second[j]) { + result[k++] = first[i++]; + } else { + result[k++] = second[j++]; + } + } + + while (i < m) { + result[k++] = first[i++]; + } + + while (j < n) { + result[k++] = second[j++]; + } + + return result; + } +} diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java new file mode 100644 index 0000000000..e5f7c3a3a5 --- /dev/null +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java @@ -0,0 +1,27 @@ +package com.baeldung.algorithms.sortedarrays; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import org.junit.jupiter.api.Test; + +public class SortedArraysUnitTest { + + @Test + public void givenTwoSortedArraysWhenMergeThenReturnMergedSortedArray() { + + int[] first = { 3, 7 }; + int[] second = { 4, 8, 11 }; + int[] result = { 3, 4, 7, 8, 11 }; + + assertArrayEquals(result, SortedArrays.merge(first, second)); + } + + @Test + public void givenTwoSortedArraysWithDuplicatesWhenMergeThenReturnMergedSortedArray() { + + int[] first = { 3, 3, 7 }; + int[] second = { 4, 8, 8, 11 }; + int[] result = { 3, 3, 4, 7, 8, 8, 11 }; + + assertArrayEquals(result, SortedArrays.merge(first, second)); + } +} From 277a1ee26639cf887488cf9fda3dd85b044c044e Mon Sep 17 00:00:00 2001 From: Vivek Balasubramaniam Date: Sat, 30 Nov 2019 18:20:38 +0530 Subject: [PATCH 2/3] How to merge sorted arrays: Renamed the variables --- .../algorithms/sortedarrays/SortedArrays.java | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java index 286eaae243..7bcf049523 100644 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java @@ -4,29 +4,29 @@ public class SortedArrays { public static int[] merge(int[] first, int[] second) { - int m = first.length; - int n = second.length; + int firstLength = first.length; + int secondLength = second.length; - int i, j, k; - i = j = k = 0; + int[] result = new int[firstLength + secondLength]; - int[] result = new int[m + n]; + int firstPosition, secondPosition, resultPosition; + firstPosition = secondPosition = resultPosition = 0; - while (i < m && j < n) { + while (firstPosition < firstLength && secondPosition < secondLength) { - if (first[i] < second[j]) { - result[k++] = first[i++]; + if (first[firstPosition] < second[secondPosition]) { + result[resultPosition++] = first[firstPosition++]; } else { - result[k++] = second[j++]; + result[resultPosition++] = second[secondPosition++]; } } - while (i < m) { - result[k++] = first[i++]; + while (firstPosition < firstLength) { + result[resultPosition++] = first[firstPosition++]; } - while (j < n) { - result[k++] = second[j++]; + while (secondPosition < secondLength) { + result[resultPosition++] = second[secondPosition++]; } return result; From 792545e6551b976e50557240980fb12314f7f77b Mon Sep 17 00:00:00 2001 From: Vivek Balasubramaniam Date: Sun, 15 Dec 2019 18:57:20 +0530 Subject: [PATCH 3/3] BAEL-3399: How to merge two sorted arrays (based on review) --- .../mergesortedarrays/SortedArrays.java | 33 ++++++++++++++++++ .../algorithms/sortedarrays/SortedArrays.java | 34 ------------------- .../SortedArraysUnitTest.java | 29 ++++++++++++++++ .../sortedarrays/SortedArraysUnitTest.java | 27 --------------- 4 files changed, 62 insertions(+), 61 deletions(-) create mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/mergesortedarrays/SortedArrays.java delete mode 100644 algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java create mode 100644 algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/mergesortedarrays/SortedArraysUnitTest.java delete mode 100644 algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/mergesortedarrays/SortedArrays.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/mergesortedarrays/SortedArrays.java new file mode 100644 index 0000000000..5d1c4af650 --- /dev/null +++ b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/mergesortedarrays/SortedArrays.java @@ -0,0 +1,33 @@ +package com.baeldung.algorithms.mergesortedarrays; + +public class SortedArrays { + + public static int[] merge(int[] foo, int[] bar) { + + int fooLength = foo.length; + int barLength = bar.length; + + int[] merged = new int[fooLength + barLength]; + + int fooPosition, barPosition, mergedPosition; + fooPosition = barPosition = mergedPosition = 0; + + while (fooPosition < fooLength && barPosition < barLength) { + if (foo[fooPosition] < bar[barPosition]) { + merged[mergedPosition++] = foo[fooPosition++]; + } else { + merged[mergedPosition++] = bar[barPosition++]; + } + } + + while (fooPosition < fooLength) { + merged[mergedPosition++] = foo[fooPosition++]; + } + + while (barPosition < barLength) { + merged[mergedPosition++] = bar[barPosition++]; + } + + return merged; + } +} diff --git a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java b/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java deleted file mode 100644 index 7bcf049523..0000000000 --- a/algorithms-miscellaneous-5/src/main/java/com/baeldung/algorithms/sortedarrays/SortedArrays.java +++ /dev/null @@ -1,34 +0,0 @@ -package com.baeldung.algorithms.sortedarrays; - -public class SortedArrays { - - public static int[] merge(int[] first, int[] second) { - - int firstLength = first.length; - int secondLength = second.length; - - int[] result = new int[firstLength + secondLength]; - - int firstPosition, secondPosition, resultPosition; - firstPosition = secondPosition = resultPosition = 0; - - while (firstPosition < firstLength && secondPosition < secondLength) { - - if (first[firstPosition] < second[secondPosition]) { - result[resultPosition++] = first[firstPosition++]; - } else { - result[resultPosition++] = second[secondPosition++]; - } - } - - while (firstPosition < firstLength) { - result[resultPosition++] = first[firstPosition++]; - } - - while (secondPosition < secondLength) { - result[resultPosition++] = second[secondPosition++]; - } - - return result; - } -} diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/mergesortedarrays/SortedArraysUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/mergesortedarrays/SortedArraysUnitTest.java new file mode 100644 index 0000000000..76eeb7b116 --- /dev/null +++ b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/mergesortedarrays/SortedArraysUnitTest.java @@ -0,0 +1,29 @@ +package com.baeldung.algorithms.mergesortedarrays; + +import static org.junit.jupiter.api.Assertions.assertArrayEquals; +import org.junit.jupiter.api.Test; + +import com.baeldung.algorithms.mergesortedarrays.SortedArrays; + +public class SortedArraysUnitTest { + + @Test + public void givenTwoSortedArrays_whenMerged_thenReturnMergedSortedArray() { + + int[] foo = { 3, 7 }; + int[] bar = { 4, 8, 11 }; + int[] merged = { 3, 4, 7, 8, 11 }; + + assertArrayEquals(merged, SortedArrays.merge(foo, bar)); + } + + @Test + public void givenTwoSortedArraysWithDuplicates_whenMerged_thenReturnMergedSortedArray() { + + int[] foo = { 3, 3, 7 }; + int[] bar = { 4, 8, 8, 11 }; + int[] merged = { 3, 3, 4, 7, 8, 8, 11 }; + + assertArrayEquals(merged, SortedArrays.merge(foo, bar)); + } +} diff --git a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java b/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java deleted file mode 100644 index e5f7c3a3a5..0000000000 --- a/algorithms-miscellaneous-5/src/test/java/com/baeldung/algorithms/sortedarrays/SortedArraysUnitTest.java +++ /dev/null @@ -1,27 +0,0 @@ -package com.baeldung.algorithms.sortedarrays; - -import static org.junit.jupiter.api.Assertions.assertArrayEquals; -import org.junit.jupiter.api.Test; - -public class SortedArraysUnitTest { - - @Test - public void givenTwoSortedArraysWhenMergeThenReturnMergedSortedArray() { - - int[] first = { 3, 7 }; - int[] second = { 4, 8, 11 }; - int[] result = { 3, 4, 7, 8, 11 }; - - assertArrayEquals(result, SortedArrays.merge(first, second)); - } - - @Test - public void givenTwoSortedArraysWithDuplicatesWhenMergeThenReturnMergedSortedArray() { - - int[] first = { 3, 3, 7 }; - int[] second = { 4, 8, 8, 11 }; - int[] result = { 3, 3, 4, 7, 8, 8, 11 }; - - assertArrayEquals(result, SortedArrays.merge(first, second)); - } -}