forked from akash-coded/core-java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge.java
More file actions
34 lines (27 loc) · 825 Bytes
/
Merge.java
File metadata and controls
34 lines (27 loc) · 825 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
public class Merge {
public static int[] mergeSortedarrays(int a[], int b[]) {
int[] m = new int[a.length + b.length];
int i = 0;
int j = 0;
int k = 0;
while (i < a.length && j < b.length) {
if (a[i] <= b[j])
m[k++] = a[i++];
else
m[k++] = b[j++];
}
while (i < a.length)
m[k++] = a[i++];
while (j < b.length)
m[k++] = b[j++];
return m;
}
public static void main(String[] args) {
int[] a = { 1, 3, 5, 7, 9 };
int[] b = { 2, 4, 6, 8, 10, 12, 14 };
System.out.println("Sorted merger array is:");
int[] c = mergeSortedarrays(a, b);
for (int i = 0; i < c.length; i++)
System.out.print(c[i] + ", ");
}
}