Wednesday 26 October 2011

implementation of merging of two array or list

/*
ALGORITHM:

1. Initialized low1=0 of A array and low2=0 of B array.
2. Repeat step 2 until low1<n && low2<n.
            a. copy element of A array into C array if element of A array is smaller than B.
            b. Other wise copy element of B array into C array.
3. Repeat if any element of A array is still left to be insert into C array i.e. low1<n
4. Repeat if any element of B array is still left to be insert into C array i.e. low2<n
*/
//program is implemented by shadab khan
#include<iostream.h>
#include<conio.h>
void main()
{
            clrscr();
            int a[5],b[5],c[10],i,j,k;
            cout<<"enter the 5 element of Ist array : \n\n";
            for(i=0;i<=4;i++)
            cin>>a[i];
            cout<<"enter the 5 element of IInd array : \n\n";
            for(i=0;i<=4;i++)
            cin>>b[i];
            i=0;j=0;k=0;
            while(i<5 && j<5)
            {
                        if(a[i]<b[j])
                        {
                                    c[k]=a[i];
                                    i++;
                                    k++;
                        }
                        else
                        {
                                    c[k]=b[j];
                                    j++;
                                    k++;
                        }
            }
            while(i<5)
            {
                        c[k]=a[i];
                        i++;
                        k++;
            }
            while(j<5)
            {
                        c[k]=b[j];
                        j++;
                        k++;
            }
            cout<<"Merged array : \n\n";
            for(i=0;i<=9;i++)
            cout<<c[i]<<endl;
            getch();
            }

No comments:

Post a Comment