[10] Create a program called Merge.java that implements a variety of the Merge function (in the Merge Sort algorithm). The program should be able to do the following: accepts two command line parameters, each specifies a text file containing the integers to be merged. The structure of the files is as follows: For both files, there will be multiple lines, each of which contains one integer. The number of lines is unknown. reads the integers from the text files into two corresponding array of integers. sorts the array from first file using InsertionSort in ascending order. sorts the array from the second file using InsertionSort in descending order. merges the two arrays into one large array sorted in descending order. There must be no resorting of this array after the merging process is completed. prints out the sorted version (ascending order)of the first array on a single line, with the integers separated by a single white space. prints out a sorted version (descending order) of the second array on a single line, with the integers separated by a single white space. prints out a sorted version (descending order) of the merged array on a single line, with the integers separated by a single white space.

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 .