Skip to content

Commit 6c5f7ce

Browse files
authored
Merge branch 'master' into merge_k_ll_improve
2 parents c1ae186 + c8e9e74 commit 6c5f7ce

File tree

3 files changed

+136
-31
lines changed

3 files changed

+136
-31
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -831,6 +831,7 @@
831831
* [CircleLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CircleLinkedListTest.java)
832832
* [CreateAndDetectLoopTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/CreateAndDetectLoopTest.java)
833833
* [MergeKSortedLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeKSortedLinkedListTest.java)
834+
* [MergeSortedArrayListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedArrayListTest.java)
834835
* [MergeSortedSinglyLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedListTest.java)
835836
* [QuickSortLinkedListTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/QuickSortLinkedListTest.java)
836837
* [ReverseKGroupTest](https://github.com/TheAlgorithms/Java/blob/master/src/test/java/com/thealgorithms/datastructures/lists/ReverseKGroupTest.java)

src/main/java/com/thealgorithms/datastructures/lists/MergeSortedArrayList.java

Lines changed: 36 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,49 +1,55 @@
11
package com.thealgorithms.datastructures.lists;
22

3-
import java.util.ArrayList;
43
import java.util.Collection;
54
import java.util.List;
65

76
/**
7+
* Utility class for merging two sorted ArrayLists of integers into a single sorted collection.
8+
*
9+
* <p>This class provides a static `merge` method to combine two pre-sorted lists of integers into a
10+
* single sorted list. It does so without modifying the input lists by adding elements from both lists in sorted order
11+
* into the result list.</p>
12+
*
13+
* <p>Example usage:</p>
14+
* <pre>
15+
* List<Integer> listA = Arrays.asList(1, 3, 5, 7, 9);
16+
* List<Integer> listB = Arrays.asList(2, 4, 6, 8, 10);
17+
* List<Integer> result = new ArrayList<>();
18+
* MergeSortedArrayList.merge(listA, listB, result);
19+
* </pre>
20+
*
21+
* <p>The resulting `result` list will be [1, 2, 3, 4, 5, 6, 7, 8, 9, 10].</p>
22+
*
23+
* <p>Note: This class cannot be instantiated as it is designed to be used only with its static `merge` method.</p>
24+
*
25+
* <p>This implementation assumes the input lists are already sorted in ascending order.</p>
26+
*
827
* @author https://github.com/shellhub
28+
* @see List
929
*/
1030
public final class MergeSortedArrayList {
11-
private MergeSortedArrayList() {
12-
}
13-
14-
public static void main(String[] args) {
15-
List<Integer> listA = new ArrayList<>();
16-
List<Integer> listB = new ArrayList<>();
17-
List<Integer> listC = new ArrayList<>();
18-
19-
/* init ListA and List B */
20-
for (int i = 1; i <= 10; i += 2) {
21-
listA.add(i);
22-
/* listA: [1, 3, 5, 7, 9] */
23-
listB.add(i + 1);
24-
/* listB: [2, 4, 6, 8, 10] */
25-
}
26-
27-
/* merge listA and listB to listC */
28-
merge(listA, listB, listC);
2931

30-
System.out.println("listA: " + listA);
31-
System.out.println("listB: " + listB);
32-
System.out.println("listC: " + listC);
32+
private MergeSortedArrayList() {
3333
}
3434

3535
/**
36-
* merge two sorted ArrayList
36+
* Merges two sorted lists of integers into a single sorted collection.
37+
*
38+
* <p>This method does not alter the original lists (`listA` and `listB`). Instead, it inserts elements from both
39+
* lists into `listC` in a way that maintains ascending order.</p>
3740
*
38-
* @param listA the first list to merge
39-
* @param listB the second list to merge
40-
* @param listC the result list after merging
41+
* @param listA The first sorted list of integers.
42+
* @param listB The second sorted list of integers.
43+
* @param listC The collection to hold the merged result, maintaining sorted order.
44+
* @throws NullPointerException if any of the input lists or result collection is null.
4145
*/
4246
public static void merge(List<Integer> listA, List<Integer> listB, Collection<Integer> listC) {
47+
if (listA == null || listB == null || listC == null) {
48+
throw new NullPointerException("Input lists and result collection must not be null.");
49+
}
50+
4351
int pa = 0;
44-
/* the index of listA */
4552
int pb = 0;
46-
/* the index of listB */
4753

4854
while (pa < listA.size() && pb < listB.size()) {
4955
if (listA.get(pa) <= listB.get(pb)) {
@@ -53,12 +59,11 @@ public static void merge(List<Integer> listA, List<Integer> listB, Collection<In
5359
}
5460
}
5561

56-
/* copy left element of listA to listC */
62+
// Add remaining elements from listA, if any
5763
while (pa < listA.size()) {
5864
listC.add(listA.get(pa++));
5965
}
60-
61-
/* copy left element of listB to listC */
66+
// Add remaining elements from listB, if any
6267
while (pb < listB.size()) {
6368
listC.add(listB.get(pb++));
6469
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
package com.thealgorithms.datastructures.lists;
2+
3+
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
5+
import static org.junit.jupiter.api.Assertions.assertTrue;
6+
7+
import java.util.ArrayList;
8+
import java.util.Arrays;
9+
import java.util.List;
10+
import org.junit.jupiter.api.Test;
11+
12+
class MergeSortedArrayListTest {
13+
14+
@Test
15+
void testMergeTwoSortedLists() {
16+
List<Integer> listA = Arrays.asList(1, 3, 5, 7, 9);
17+
List<Integer> listB = Arrays.asList(2, 4, 6, 8, 10);
18+
List<Integer> result = new ArrayList<>();
19+
20+
MergeSortedArrayList.merge(listA, listB, result);
21+
22+
List<Integer> expected = Arrays.asList(1, 2, 3, 4, 5, 6, 7, 8, 9, 10);
23+
assertEquals(expected, result, "Merged list should be sorted and contain all elements from both input lists.");
24+
}
25+
26+
@Test
27+
void testMergeWithEmptyList() {
28+
List<Integer> listA = Arrays.asList(1, 2, 3);
29+
List<Integer> listB = new ArrayList<>(); // Empty list
30+
List<Integer> result = new ArrayList<>();
31+
32+
MergeSortedArrayList.merge(listA, listB, result);
33+
34+
List<Integer> expected = Arrays.asList(1, 2, 3);
35+
assertEquals(expected, result, "Merged list should match listA when listB is empty.");
36+
}
37+
38+
@Test
39+
void testMergeWithBothEmptyLists() {
40+
List<Integer> listA = new ArrayList<>(); // Empty list
41+
List<Integer> listB = new ArrayList<>(); // Empty list
42+
List<Integer> result = new ArrayList<>();
43+
44+
MergeSortedArrayList.merge(listA, listB, result);
45+
46+
assertTrue(result.isEmpty(), "Merged list should be empty when both input lists are empty.");
47+
}
48+
49+
@Test
50+
void testMergeWithDuplicateElements() {
51+
List<Integer> listA = Arrays.asList(1, 2, 2, 3);
52+
List<Integer> listB = Arrays.asList(2, 3, 4);
53+
List<Integer> result = new ArrayList<>();
54+
55+
MergeSortedArrayList.merge(listA, listB, result);
56+
57+
List<Integer> expected = Arrays.asList(1, 2, 2, 2, 3, 3, 4);
58+
assertEquals(expected, result, "Merged list should correctly handle and include duplicate elements.");
59+
}
60+
61+
@Test
62+
void testMergeWithNegativeAndPositiveNumbers() {
63+
List<Integer> listA = Arrays.asList(-3, -1, 2);
64+
List<Integer> listB = Arrays.asList(-2, 0, 3);
65+
List<Integer> result = new ArrayList<>();
66+
67+
MergeSortedArrayList.merge(listA, listB, result);
68+
69+
List<Integer> expected = Arrays.asList(-3, -2, -1, 0, 2, 3);
70+
assertEquals(expected, result, "Merged list should correctly handle negative and positive numbers.");
71+
}
72+
73+
@Test
74+
void testMergeThrowsExceptionOnNullInput() {
75+
List<Integer> listA = null;
76+
List<Integer> listB = Arrays.asList(1, 2, 3);
77+
List<Integer> result = new ArrayList<>();
78+
79+
List<Integer> finalListB = listB;
80+
List<Integer> finalListA = listA;
81+
List<Integer> finalResult = result;
82+
assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(finalListA, finalListB, finalResult), "Should throw NullPointerException if any input list is null.");
83+
84+
listA = Arrays.asList(1, 2, 3);
85+
listB = null;
86+
List<Integer> finalListA1 = listA;
87+
List<Integer> finalListB1 = listB;
88+
List<Integer> finalResult1 = result;
89+
assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(finalListA1, finalListB1, finalResult1), "Should throw NullPointerException if any input list is null.");
90+
91+
listA = Arrays.asList(1, 2, 3);
92+
listB = Arrays.asList(4, 5, 6);
93+
result = null;
94+
List<Integer> finalListA2 = listA;
95+
List<Integer> finalListB2 = listB;
96+
List<Integer> finalResult2 = result;
97+
assertThrows(NullPointerException.class, () -> MergeSortedArrayList.merge(finalListA2, finalListB2, finalResult2), "Should throw NullPointerException if the result collection is null.");
98+
}
99+
}

0 commit comments

Comments
 (0)