0% found this document useful (0 votes)
1 views2 pages

Merging and Merging Two Sorted Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

Merging and Merging Two Sorted Arrays

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

MERGING AND MERGING TWO SORTED ARRAYS

class merging
{
public static void main()
{
int a[]={11,12,13,14,15};// array initialisation
int b[]={45,67,89};//array initialisation
int c[]=new int[a.length+b.length];// array declaration
for(int i=0;i<a.length;i++)
c[i]=a[i];
int k=a.length;
for(int i=0;i<b.length;i++)
c[k++]=b[i];
for(int i=0;i<c.length;i++)
System.out.print(c[i]+" ");
}
}
class mergingsorted
{
public static void main()
{
int a[]={11,22,33,45,89,90,100};// array initialisation
int b[]={4,6,120};//array initialisation
int c[]=new int[a.length+b.length];
int i=0,j=0,m=a.length,n=b.length;
int k=0;
while(i<m && j<n)
{
if(a[i]<b[j])
c[k++]=a[i++];
else
c[k++]=b[j++];
}
while(i<m)
c[k++]=a[i++];
while(j<n)
c[k++]=b[j++];
for(i=0;i<c.length;i++)
System.out.print(c[i]+" ");
}
}

You might also like