forked from akshay2742/Hacktoberfest2022
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMerge_sort_array.java
More file actions
45 lines (45 loc) · 1.14 KB
/
Merge_sort_array.java
File metadata and controls
45 lines (45 loc) · 1.14 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
//Inplace merging of two sorted arrays
import java.io.*;
class Merge_sort_array
{
public static void main(String args[]) throws IOException
{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int a1,m,n,b1;
a1=Integer.parseInt(br.readLine());
b1=Integer.parseInt(br.readLine());
m=Integer.parseInt(br.readLine());
n=Integer.parseInt(br.readLine());
int a[]=new int[a1];
int b[]=new int[b1];
int i,j=0,k;
for(i=0;i<a1;i++)
{
a[i]=Integer.parseInt(br.readLine());
}
for(i=0;i<b1;i++)
{
b[i]=Integer.parseInt(br.readLine());
}
for(i=m;i<a1;i++)
{
a[i]=b[j];
j++;
}
for(i=1;i<a1;i++)
{
k=a[i];
j=i-1;
while(j>=0 && a[j]>k)
{
a[j + 1] = a[j];
j = j - 1;
}
a[j + 1] = k;
}
for(i=0;i<m+n;i++)
{
System.out.print(a[i]+" ");
}
}
}