From 792545e6551b976e50557240980fb12314f7f77b Mon Sep 17 00:00:00 2001 From: Vivek Balasubramaniam Date: Sun, 15 Dec 2019 18:57:20 +0530 Subject: [PATCH] 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)); - } -}