Thursday, August 29, 2019

C++ Language Program for Sort Two Array in Merge Sort.


BACHELOR OF COMPUTER APPLICATION (B.C.A.) FIRST SEMESTER

Here you will get simple C++ program 
C++ Language Program Sort Two Array in Merge Sort.
IT IS THE SMALLEST LOGIC USED FOR CODING OF THIS PROGRAM.
IN WHICH YOU CAN CREATE OWN VARIABLES AND FUNCTIONS.

User will input will be an unsorted form in Two Array(A&B) and that C Array is sorted by Merge Sort,then it will be display on screen.


The program coding is given below.
#include <iostream.h>
#include <conio.h>
class Abc
{
int a[10],b[10],c[10],R,S,N,NA,NB,PTR;
public:
void getdata()
{
cout<<"Enter Array A Size: ";
cin>>R;
cout<<"Enter Array A: ";
for (int i=1; i<=R; i++)
{
cin>>a[i];
}
cout<<"Enter Array B Size: ";
cin>>S;
cout<<"Enter Array B: ";
for (int j=1; j<=S; j++)
{
cin>>b[j];
}
N=R+S;
}
void msort()
{
NA=NB=PTR=1;
while(NA<=R && NB<=S)
{
if (a[NA]<b[NB])
{
c[PTR]=a[NA];
PTR++;
NA++;
}
else
{
c[PTR]=b[NB];
PTR++;
NB++;
}
}
if (NA>R)
{
for (int k=0; k<=S-NB; k++)
{
c[PTR+k]=b[NB+k];
}
}
else
{
for (int k=0; k<=R-NA; k++)
{
c[PTR+k]=a[NA+k];
}
}
}
void display()
{
cout<<"\nArray C:";
for (int k=1; k<=N; k++)
{
cout<<c[k]<<" ";
}
}
};
void main()
{
clrscr();
Abc a1;
a1.getdata();
a1.msort();
a1.display();
getch();
}
Output will be given after you compile it.

No comments:

Post a Comment