-
Notifications
You must be signed in to change notification settings - Fork 0
/
MergeSortedArray.java
43 lines (39 loc) · 1005 Bytes
/
MergeSortedArray.java
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
class MergeSortedArray {
public static final int NA = -1;
public void merge2one(int A[], int B[], int n, int m) {
int i,j,k;
j = n+m-1;
for (i=n+m-1; i>=0; i--) {
if (A[i] != NA) {
A[j] = A[i];
j--;
}
}
i = m;
j = 0;
k = 0;
while (k < n+m) {
if ((i < n+m && A[i] < B[j]) || j==m) {
A[k] = A[i];
k++;
i++;
} else {
A[k] = B[j];
k++;
j++;
}
}
}
public static void main(String args[]) {
MergeSortedArray msa = new MergeSortedArray();
int A[] = {2,NA,4,6,8,NA,10,NA,NA,14};
int n = 6;
int B[] = {1,3,5,17};
int m = 4;
msa.merge2one(A,B,n,m);
System.out.println("Merged Array ");
for(int i=0; i< n+m; i++) {
System.out.print(A[i]+" ");
}
}
}