-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_two_Sorted_array
More file actions
41 lines (40 loc) · 997 Bytes
/
Merge_two_Sorted_array
File metadata and controls
41 lines (40 loc) · 997 Bytes
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
public class Merge_two_Sorted_array {
public static void main(String[] args) {
int[] a = { 3, 5, 7, 9, 11 };
int[] b = { 1, 2, 3, 4, 6, 7 };
int[] ans = func(a, b);
for (int i = 0; i < ans.length; i++) {
System.out.println(ans[i]);
}
}
public static int[] func(int[] a, int[] b) {
int length = a.length + b.length;
int[] merge = new int[length];
int i = 0;
int j = 0;
int k = 0;
while(i<a.length && j < b.length){
if(a[i] <= b[j]){
merge[k] = a[i];
i++;
k++;
}
else{
merge[k] = b[j];
j++;
k++;
}
}
while (i<a.length) {
merge[k] = a[i];
k++;
i++;
}
while (j<b.length) {
merge[k] = b[j];
j++;
k++;
}
return merge;
}
}