BAEL-3399: How to merge two sorted arrays (based on review)
This commit is contained in:
parent
277a1ee266
commit
792545e655
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -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;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -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));
|
||||||
|
}
|
||||||
|
}
|
|
@ -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));
|
|
||||||
}
|
|
||||||
}
|
|
Loading…
Reference in New Issue