Skip to content

Commit ebd6c39

Browse files
realDuYuanChaogithub-actions
andauthored
merge sorted array (examplehub#143)
* add test * merge sorted array * Formatted with Google Java Formatter Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
1 parent c8d3c30 commit ebd6c39

File tree

4 files changed

+156
-0
lines changed

4 files changed

+156
-0
lines changed
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import java.util.Arrays;
4+
5+
/** https://leetcode.com/problems/merge-sorted-array/ */
6+
public class MergeSortedArray {
7+
public static void solution1(int[] nums1, int m, int[] nums2, int n) {
8+
int[] temp = new int[m + n];
9+
int firstIndex = 0;
10+
int secondIndex = 0;
11+
int index = 0;
12+
while (firstIndex < m && secondIndex < n) {
13+
if (nums1[firstIndex] <= nums2[secondIndex]) {
14+
temp[index++] = nums1[firstIndex++];
15+
} else {
16+
temp[index++] = nums2[secondIndex++];
17+
}
18+
}
19+
while (firstIndex < m) {
20+
temp[index++] = nums1[firstIndex++];
21+
}
22+
while (secondIndex < n) {
23+
temp[index++] = nums2[secondIndex++];
24+
}
25+
System.arraycopy(temp, 0, nums1, 0, m + n);
26+
}
27+
28+
public static void solution2(int[] nums1, int m, int[] nums2, int n) {
29+
System.arraycopy(nums2, 0, nums1, m, n);
30+
Arrays.sort(nums1);
31+
}
32+
33+
public static void solution3(int[] nums1, int m, int[] nums2, int n) {
34+
int[] sorted = new int[m + n];
35+
int firstIndex = 0;
36+
int secondIndex = 0;
37+
int index = 0;
38+
while (firstIndex < m || secondIndex < n) {
39+
if (firstIndex == m) {
40+
sorted[index++] = nums2[secondIndex++];
41+
} else if (secondIndex == n) {
42+
sorted[index++] = nums1[firstIndex++];
43+
} else if (nums1[firstIndex] <= nums2[secondIndex]) {
44+
sorted[index++] = nums1[firstIndex++];
45+
} else {
46+
sorted[index++] = nums2[secondIndex++];
47+
}
48+
}
49+
System.arraycopy(sorted, 0, nums1, 0, m + n);
50+
}
51+
52+
public static void solution4(int[] nums1, int m, int[] nums2, int n) {
53+
int firstIndex = m - 1;
54+
int secondIndex = n - 1;
55+
int index = m + n - 1;
56+
while (firstIndex >= 0 || secondIndex >= 0) {
57+
if (firstIndex == -1) {
58+
nums1[index--] = nums2[secondIndex--];
59+
} else if (secondIndex == -1) {
60+
nums1[index--] = nums1[firstIndex--];
61+
} else if (nums1[firstIndex] >= nums2[secondIndex]) {
62+
nums1[index--] = nums1[firstIndex--];
63+
} else {
64+
nums1[index--] = nums2[secondIndex--];
65+
}
66+
}
67+
}
68+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package com.examplehub.leetcode.easy;
2+
3+
import static org.junit.jupiter.api.Assertions.*;
4+
5+
import java.util.Arrays;
6+
import org.junit.jupiter.api.Test;
7+
8+
class MergeSortedArrayTest {
9+
@Test
10+
void testSolution1() {
11+
int[] nums1 = {1, 2, 3, 0, 0, 0};
12+
int[] nums2 = {2, 5, 6};
13+
MergeSortedArray.solution1(nums1, 3, nums2, 3);
14+
assertEquals("[1, 2, 2, 3, 5, 6]", Arrays.toString(nums1));
15+
16+
nums1 = new int[] {1};
17+
nums2 = new int[] {};
18+
MergeSortedArray.solution1(nums1, 1, nums2, 0);
19+
assertEquals("[1]", Arrays.toString(nums1));
20+
21+
nums1 = new int[1];
22+
nums2 = new int[] {1};
23+
MergeSortedArray.solution1(nums1, 0, nums2, 1);
24+
assertEquals("[1]", Arrays.toString(nums1));
25+
}
26+
27+
@Test
28+
void testSolution2() {
29+
int[] nums1 = {1, 2, 3, 0, 0, 0};
30+
int[] nums2 = {2, 5, 6};
31+
MergeSortedArray.solution2(nums1, 3, nums2, 3);
32+
assertEquals("[1, 2, 2, 3, 5, 6]", Arrays.toString(nums1));
33+
34+
nums1 = new int[] {1};
35+
nums2 = new int[] {};
36+
MergeSortedArray.solution2(nums1, 1, nums2, 0);
37+
assertEquals("[1]", Arrays.toString(nums1));
38+
39+
nums1 = new int[1];
40+
nums2 = new int[] {1};
41+
MergeSortedArray.solution2(nums1, 0, nums2, 1);
42+
assertEquals("[1]", Arrays.toString(nums1));
43+
}
44+
45+
@Test
46+
void testSolution3() {
47+
int[] nums1 = {1, 2, 3, 0, 0, 0};
48+
int[] nums2 = {2, 5, 6};
49+
MergeSortedArray.solution3(nums1, 3, nums2, 3);
50+
assertEquals("[1, 2, 2, 3, 5, 6]", Arrays.toString(nums1));
51+
52+
nums1 = new int[] {1};
53+
nums2 = new int[] {};
54+
MergeSortedArray.solution3(nums1, 1, nums2, 0);
55+
assertEquals("[1]", Arrays.toString(nums1));
56+
57+
nums1 = new int[1];
58+
nums2 = new int[] {1};
59+
MergeSortedArray.solution3(nums1, 0, nums2, 1);
60+
assertEquals("[1]", Arrays.toString(nums1));
61+
}
62+
63+
@Test
64+
void testSolution4() {
65+
int[] nums1 = {1, 2, 3, 0, 0, 0};
66+
int[] nums2 = {2, 5, 6};
67+
MergeSortedArray.solution4(nums1, 3, nums2, 3);
68+
assertEquals("[1, 2, 2, 3, 5, 6]", Arrays.toString(nums1));
69+
70+
nums1 = new int[] {1};
71+
nums2 = new int[] {};
72+
MergeSortedArray.solution4(nums1, 1, nums2, 0);
73+
assertEquals("[1]", Arrays.toString(nums1));
74+
75+
nums1 = new int[1];
76+
nums2 = new int[] {1};
77+
MergeSortedArray.solution4(nums1, 0, nums2, 1);
78+
assertEquals("[1]", Arrays.toString(nums1));
79+
}
80+
}

src/test/java/com/examplehub/sorts/QuickSortEasyPartitionTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ void testQuickSort() {
2121
int[] ints = RandomUtils.randomInts(-50, 50, 100);
2222
sort.sort(ints);
2323
assertTrue(SortUtils.isSorted(ints));
24+
25+
ints = new int[] {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
26+
sort.sort(ints);
27+
assertTrue(SortUtils.isSorted(ints));
2428
}
2529

2630
@Test

src/test/java/com/examplehub/sorts/QuickSortTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ void testQuickSort() {
2222
int[] ints = RandomUtils.randomInts(-50, 50, 100);
2323
sort.sort(ints);
2424
assertTrue(SortUtils.isSorted(ints));
25+
26+
ints = new int[] {0, 1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
27+
sort.sort(ints);
28+
assertTrue(SortUtils.isSorted(ints));
2529
}
2630

2731
@Test

0 commit comments

Comments
 (0)