#include<iostream>
using namespace std;
int n;
class Array
{
public:
int ar[50],br[50];
void getval()
{
cout<<"Enter size of the array: ";
cin>>n;
cout<<"Enter array elements: "<<endl;
for(int i=0;i<n;i++)
cin>>ar[i];
}
void mergesort(Array obj)
{
for(int i=0;i<n;i++)
br[i]=obj.ar[i];
msort(br,0,n-1);
}
void merge(int br[],int low,int mid,int high)
{
int i=low,j=mid+1,k=low,l,temp[n],m=low;
while(i<=mid && j<=high)
{
if(br[i]<=br[j])
{
temp[k]=br[i];
i++;
}
else
{
temp[k]=br[j];
j++;
}
k++;
}
if(i>mid)
{
while(j<=high)
{
temp[k]=br[j];
k++;
j++;
}
}
else
{
while(i<=mid)
{
temp[k]=br[i];
k++;
i++;
}
}
for(l=low;l<k;l++)
{
br[l]=temp[l];
}
}
void msort(int br[],int low,int high)
{
if(low<high)
{
int mid=(low+high)/2;
msort(br,low,mid);
msort(br,mid+1,high);
merge(br,low,mid,high);
}
}
void print()
{
cout<<"The sorted array is: "<<endl;
for(int i=0;i<n;i++)
cout<<br[i]<<" ";
}
};
Array obj1,obj2;
int main()
{
obj1.getval();
obj2.mergesort(obj1);
obj2.print();
}
Merge.cpp
Subscribe to:
Post Comments (Atom)
-
Capgemini has issued a notification for the recruitment of Architect Posts Vacancy at Various posts. Interested candidates can apply by At...
-
#include<iostream> using namespace std; int n; class Array { public: int ar[50]; void getval() { cout<...
-
MasterCard has issued a latest notification for the recruitment of Manager, Specialist & Engineer Post Vacancy at Various posts.Othe...
No comments:
Post a Comment