-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArrayMergeDat.java
More file actions
54 lines (51 loc) · 1.41 KB
/
ArrayMergeDat.java
File metadata and controls
54 lines (51 loc) · 1.41 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
import java.util.Scanner;
public class ArrayMergeDat {
public static void main(String[] args) {
Scanner obj=new Scanner(System.in);
System.out.println("Enter a size of array 1 :- ");
int n=obj.nextInt();
System.out.println("Enter a size of array 2 :- ");
int m=obj.nextInt();
int array[]=new int[n];
int array1[]=new int[m];
System.out.println("Enter a Array 1 :- ");
for(int i=0;i<n;i++){
array[i]=obj.nextInt();
}
System.out.println("Enter a Array 2 :- ");
for(int i=0;i<m;i++){
array1[i]=obj.nextInt();
}
merge(array,array1,n,m);
obj.close();
}
public static void merge(int a[],int b[],int n,int m){
int merge[]=new int[n+m];
int i=0,j=0,k=0;
while(i<n && j<m){
if(a[i]>=b[j]){
merge[k]=b[j];
j++;
}
else{
merge[k]=a[i];
i++;
}
k++;
}
while(i<n){
merge[k]=a[i];
k++;
i++;
}
while(j<m){
merge[k]=b[j];
k++;
j++;
}
System.out.println("Merge array :- ");
for(i=0;i<m+n;i++){
System.out.print(merge[i]+ " ");
}
}
}