Respuesta :
Answer:
import java.util.*;
import java.io.*;
class Merge
{
public static void main (String[] args) throws FileNotFoundException
{
File file1 = new File(args[0]);
File file2 = new File(args[1]);
Scanner in;
in = new Scanner(file1);
int i, p, q;
int a[] = new int[100];
int b[] = new int[100];
int c[] = new int[100];
i = 0;
while(in.hasNextInt())
{
a[i] = in.nextInt();
i++
}
int n = i;
i = 1;
while( i < n )
{
int t = a[i];
for(p=i-1; p>=0 && t<a[p] ; p--)
a[p+1] = a[p];
a[p+1] = t;
i++;
}
in = new Scanner(file2);
i=0;
while(in.hasNextInt())
{
b[i] = in.nextInt();
i++;
}
int m = i;
for(i=1; i<m; i++)
{
int t = b[i];
for(p=i-1; p>=0 && t>b[p] ; p--)
b[p+1] = b[p];
b[p+1] = t;
}
i=n-1; p=0; q=0;
do
{
if(a[i]>b[p])
c[q++] = a[i--];
else
c[q++] = b[p++];
} while(i>=0 && p<m);
do
{
c[q++] = a[i--];
} while(i>=0)
do
{
c[q++] = b[p++];
} while(p<m);
System.out.println("1st array: ");
for(i=0; i<n; i++)
System.out.print(a[i] + " ");
System.out.println("\n 2nd array: ");
for(i=0; i<m; i++)
System.out.print(b[i] + " ");
System.out.println("\n Sorted Array: ");
for(i=0; i<q; i++)
System.out.print(c[i] + " ");
}
}
Explanation:
- First of all create objects of File i.e. file1 and file2.
- Read from first file and sort the first array .
- Read from second file and sort the second array
- Merge two sub-arrays in the form of concatenation.
- Finally display the arrays .