import java.util.*;
class mergesort
{
public static void main(String[] args)
{
int i,j,k,n,m,g,temp;
Scanner ms = new Scanner(System.in);
System.out.println("\nEnter Size Of first Array :");
m=ms.nextInt();
int[] a =new int[m]; // Creation of array with given size
System.out.println("\nEnter unsorted first Array :");
for(i=0;i<m;i++)
{
System.out.printf("\n Element a[%d] :",i);
a[i]=ms.nextInt();
}
for(i=0;i<m-1;i++)
{
for(j=0;j<m-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print("\n Sorted Array A elements :");
for(i=0;i<m;i++)
{
System.out.print( a[i] +" ");
}
System.out.println("\nEnter Size Of second Array :");
n=ms.nextInt();
int[] b=new int[n]; // Creation of array with given size
int[] c=new int[m+n]; // Creation of array with given size
System.out.println("\nEnter unsorted second Array :");
for(j=0;j<n;j++)
{
System.out.printf("\n Element b[%d] :",j);
b[j]=ms.nextInt();
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
System.out.print("\n Sorted Array B elements :");
for(j=0;j<n;j++)
{
System.out.print( b[j] +" ");
}
i=0;j=0;
for(k=0;k<m+n;k++)
{
if(j>=n||(i<m && (a[i] <=b[j])))
{
c[k]=a[i];
i=i+1;
}
else if(i>=m||(j<n && (a[i] >b[j])))
{
c[k]=b[j];
j=j+1;
}
}
System.out.print("\n Sorted Array elements :");
for(k=0;k<m+n;k++)
{
System.out.println( c[k] +" ");
}
}
}
output:
class mergesort
{
public static void main(String[] args)
{
int i,j,k,n,m,g,temp;
Scanner ms = new Scanner(System.in);
System.out.println("\nEnter Size Of first Array :");
m=ms.nextInt();
int[] a =new int[m]; // Creation of array with given size
System.out.println("\nEnter unsorted first Array :");
for(i=0;i<m;i++)
{
System.out.printf("\n Element a[%d] :",i);
a[i]=ms.nextInt();
}
for(i=0;i<m-1;i++)
{
for(j=0;j<m-1-i;j++)
{
if(a[j]>a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]=temp;
}
}
}
System.out.print("\n Sorted Array A elements :");
for(i=0;i<m;i++)
{
System.out.print( a[i] +" ");
}
System.out.println("\nEnter Size Of second Array :");
n=ms.nextInt();
int[] b=new int[n]; // Creation of array with given size
int[] c=new int[m+n]; // Creation of array with given size
System.out.println("\nEnter unsorted second Array :");
for(j=0;j<n;j++)
{
System.out.printf("\n Element b[%d] :",j);
b[j]=ms.nextInt();
}
for(i=0;i<n-1;i++)
{
for(j=0;j<n-1-i;j++)
{
if(b[j]>b[j+1])
{
temp=b[j];
b[j]=b[j+1];
b[j+1]=temp;
}
}
}
System.out.print("\n Sorted Array B elements :");
for(j=0;j<n;j++)
{
System.out.print( b[j] +" ");
}
i=0;j=0;
for(k=0;k<m+n;k++)
{
if(j>=n||(i<m && (a[i] <=b[j])))
{
c[k]=a[i];
i=i+1;
}
else if(i>=m||(j<n && (a[i] >b[j])))
{
c[k]=b[j];
j=j+1;
}
}
System.out.print("\n Sorted Array elements :");
for(k=0;k<m+n;k++)
{
System.out.println( c[k] +" ");
}
}
}
output:
No comments:
Post a Comment