Thursday 19 January 2017

Merge Sort...

Write a program that inputs the names of people in to different arrays, A and B. Array A has N number of names while array B has M number of names, with no duplicates in either of them. Merge arrays A and B in to a single array C, such that the resulting array is stored alphabetically.
Display all the three arrays, A, B and C, stored alphabetically.
Test your program for the given data and some random data.

SAMPLE DATA
INPUT
Enter number of names in Array A, N = 2
Enter number of names in Array A, B = 2
First array:           (A)
Suman
Anil

Second array:                  (B)
Usha
Sachin
John

OUTPUT
Stored Merged array:       (C)
Anil
John
Sachin
Suman
Usha

Stored First array:           (A)
Anil
Suman

Stored second array:        (B)
John
Sachin

Usha


Solution:-

import java.util.*;
class merge
{
     String str1[],str2[],str3[];
     int n,m;
     String s;
     int i,j;     
     void input()//input values.
     {
           Scanner sc=new Scanner(System.in);
           System.out.println("Number of Names for first array:");
           n=sc.nextInt();
           System.out.println("Number of Names for second array:");
           m=sc.nextInt();
           str1=new String[n];
           str2=new String[m];
           str3=new String[n+m];
           System.out.println("Names for 1st array:");
           for(i=0;i< n;i++)
           {   
                System.out.print("Name:");
                str1[i]=sc.next();
           }
           System.out.println("Names for 2nd array:");
           for(i=0;i< m;i++)
           {
                System.out.print("Name:");
                str2[i]=sc.next();
           }
     }
     void sort()//sort arrays.
     {
           int flag;
           for(i=0;i< n;i++)
           {
                flag=0;
                for(j=0;j< n-i-1;j++)
                {
                     if(str1[j].compareTo(str1[j+1]) >0)
                     {
                           s=str1[j];
                           str1[j]=str1[j+1];
                           str1[j+1]=s;
                           flag=1;
                     }
                }
                if(flag==0)
                break;
           }
           for(i=0;i< m;i++)
           {
                flag=0;
                for(j=0;j< m-i-1;j++)
                {
                     if(str2[j].compareTo(str2[j+1]) >0)
                     {
                           s=str2[j];
                           str2[j]=str2[j+1];
                           str2[j+1]=s;
                           flag=1;
                     }
                }
                if(flag==0)
                break;
           }
     }
     void combine()//merge the two array.
     {
           int x=0;
           i=0;
           j=0;
           while(i!=n && j!=m)
           {
                if(str1[i].compareTo(str2[j])<=0)
           str3[x++]=str1[i++];
                else
           str3[x++]=str2[j++];
           }
           if(i==n)
           {
                for( ;j< m;j++)
                str3[x++]=str2[j];
           }
           else
           {
                for(;i< n;i++)
                str3[x++]=str1[i];
           }
     }
     void display()
     {
           System.out.println("\nSorted Merged Names\n");
           for(i=0;i< m+n;i++)
           System.out.println(str3[i]);
           System.out.println("\nFirst array names\n");
           for(i=0;i< n;i++)
           System.out.println(str1[i]);
           System.out.println("\nSecond array names\n");
           for(i=0;i< m;i++)
           System.out.println(str2[i]);
     }
     public static void main(String args[])
     {
     merge obj=new merge();
     obj.input();
     obj.sort();
     obj.combine();
     obj.display();
     }
}


No comments:

Post a Comment

Goldbach Number ISC 2018

Question: A Goldbach number is a positive even integer that can be expressed as the sum of two odd primes. Note: All even integer numbers ...