-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMergeSortedArray.java
More file actions
43 lines (42 loc) · 1.23 KB
/
MergeSortedArray.java
File metadata and controls
43 lines (42 loc) · 1.23 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class MergeSortedArray {
public static void main(String[] args) {
int m = 0;
int n = 1;
int[] nums1 = {0};
int[] nums2 = {1};
merge(nums1,m,nums2,n);
for(int i = 0;i<nums1.length;i++){
System.out.print(nums1[i]);
}
}
public static void merge(int[] nums1, int m, int[] nums2, int n) {
int[] temp = new int[nums1.length];
for (int i = 0; i < nums1.length; i++) {
temp[i] = nums1[i];
}
int i = 0, j = 0;
if (m == 0) {
for (int z = 0; z < nums2.length; z++) {
nums1[z] = nums2[z];
}
} else {
for (int k = 0; k < m + n; k++) {
if (i < m && j < n) {
if (temp[i] <= nums2[j]) {
nums1[k] = temp[i];
i++;
} else {
nums1[k] = nums2[j];
j++;
}
} else if (i < m) {
nums1[k] = temp[i];
i++;
} else if (j < n) {
nums1[k] = nums2[j];
j++;
}
}
}
}
}