-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathMergeTwoSortedArrayInSortedForm1.java
52 lines (52 loc) · 1.15 KB
/
MergeTwoSortedArrayInSortedForm1.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
44
45
46
47
48
49
50
51
52
// WAP to merge two sorted array in the sorted form.
import java.util.Scanner;
public class MergeTwoSortedArrayInSortedForm1{
public static int[] mergedArray(int a[],int b[])
{
int c[] = new int[a.length+b.length];
int i=0,j=0,k=0;
while(j<a.length && k<b.length)
{
if(a[j]<b[k])
{
c[i++]=a[j++];
}
else
{
c[i++]=b[k++];
}
}
while(j<a.length)
{
c[i++]=a[j++];
}
while(k<b.length)
{
c[i++]=b[k++];
}
return c;
}
public static int[] readArr()
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Size of Array : ");
int n = sc.nextInt();
int arr[] = new int[n];
System.out.println("Enter the "+n+" sorted elements of Array : ");
for(int i=0;i<n;i++)
{
arr[i] = sc.nextInt();
}
return arr;
}
public static void main(String[] args) {
int a[] = readArr();
int b[] = readArr();
int c[] = mergedArray(a,b);
System.out.print("The Sorted Merged Array is : ");
for(int i=0;i<c.length;i++)
{
System.out.print(c[i]+" ");
}
}
}