-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmerge.java
More file actions
109 lines (85 loc) · 2.21 KB
/
merge.java
File metadata and controls
109 lines (85 loc) · 2.21 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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
/*
PROBLEM STATEMENT:-Assume we have two input and two output tapes to perform the sorting. The internal memory
can hold and sort m records at a time. Write a program in java for external sorting. Find out
time complexity.
*/
import java.util.Scanner;
class merge
{
static int []a,b,c;
static int n1,n2;
static void accept()
{
Scanner scan=new Scanner(System.in);
int temp;
System.out.print("\nenter the size of first array : ");
n1=scan.nextInt();
System.out.print("\nenter the size of second array : ");
n2=scan.nextInt();
a=new int[n1];
b=new int[n2];
System.out.println("\nenter the element for first array : ");
for(int i=0;i<n1;i++)
a[i]=scan.nextInt();
System.out.println("\nenter the element for second array : ");
for(int i=0;i<n2;i++)
b[i]=scan.nextInt();
for(int i=0;i<n1-1;i++)
{
for(int j=i+1;j<n1;j++)
{
if(a[i]>a[j])
{
temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}
}
for(int i=0;i<n2-1;i++)
{
for(int j=i+1;j<n2;j++)
{
if(b[i]>b[j])
{
temp=b[i];
b[i]=b[j];
b[j]=temp;
}
}
}
}
static void sort() //merge sort
{
c=new int[n1+n2];
int i=0,j=0,k=0;
while(i<n1 && j<n2)
{
if(a[i]==b[j])
{
c[k++]=a[i++];
c[k++]=b[j++];
}
if(a[i]>b[j])
{
c[k++]=b[j++];
}
else if(a[i]<b[j])
{
c[k++]=a[i++];
}
}
while(i<n1)
c[k++]=a[i++];
while(j<n2)
c[k++]=b[j++];
System.out.print("sorted array : ");
for(int l=0;l<n1+n2;l++)
System.out.print(c[l]+" ");
}
public static void main(String args[])
{
accept();
sort();
}
}