-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmergeTwoSortedArray.java
More file actions
56 lines (42 loc) · 1.27 KB
/
mergeTwoSortedArray.java
File metadata and controls
56 lines (42 loc) · 1.27 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
44
45
46
47
48
49
50
51
52
53
54
55
56
import java.lang.reflect.Array;
public class mergeTwoSortedArray{
public static void main (String[] args) {
System.out.println("Welcome to Merge Two Sorted Array");
int[] Array1 = arrayInputUtility.inputArray();
int[] Array2 = arrayInputUtility.inputArray();
int[] result = mergeArray(Array1, Array2);
displayArr(result);
}
public static int[] mergeArray(int[]Array1, int[] Array2){
int[] NewArray = new int[Array1.length + Array2.length];
int i = 0, j=0, k=0;
while (i < Array1.length && j < Array2.length) {
if (Array1[i] <= Array2[j]) {
NewArray[k] = Array1[i];
i++;
}else{
NewArray[k] = Array2[j];
j++;
}
k++;
}
while (i < Array1.length) {
NewArray[k] = Array1[i];
i++;
k++;
}
while (j < Array2.length) {
NewArray[k] = Array2[j];
j++;
k++;
}
return NewArray;
}
public static void displayArr(int[] Array){
int i = 0;
while (i < Array.length) {
System.out.print(Array[i] + " ");
i++;
}
}
}