Question
Hello, I need three files in the following C++ program. i need someone to provide mergesort.cpp, mergesort_test.cpp and mergesort.h. from following program. Requested files :
Hello,
I need three files in the following C++ program. i need someone to provide mergesort.cpp, mergesort_test.cpp and mergesort.h. from following program.
Requested files: Mergesort.h, Mergesort.cpp, Mergesort_test.cpp
#include
#include
using namespace std;
//here l is left index m is middle r is right index
void mergefunction(int a[], int l, int m, int r)
{
int k;
//two temp array and their size
int xs=m - l + 1;//first array size
int ys=r-m;//second array size
int X[m - l + 1], Y[r - m];
//X is first part and copy data to it and Y is next, data copy to it
for (int i = 0; i < xs; i++)
X[i] = a[l + i];
for (int j = 0; j < ys; j++)
Y[j] = a[m + 1+ j];
//merge these array in a[]
int i = 0; // Initial index of X
int j = 0; // Initial index of Y
k = l; // Initial index of a
while (i < xs && j < ys)
{
if (X[i] <= Y[j])
{
a[k] = X[i];
i++;
}
else
{
a[k] = Y[j];
j++;
}
k++;
}
//if data is left of Y then copy remaining
while (j < ys)
{
a[k] = Y[j];
j++;
k++;
}
//if data is left of X they copy remaining
while (i < xs)
{
a[k] = X[i];
i++;
k++;
}
}
//l is lowest index h is highest index
void mergesort(int a[], int l, int h)
{
if (l < h)
{
//new middle
int m = l+(h-l)/2;
// call mergesort for both part
mergesort(a, l, m);
mergesort(a, m+1, h);
//call merge function to merge
mergefunction(a, l, m, h);
}
}
int main()
{
ifstream ofile;
int x,cnt=0;//cnt will tell the number of element in file
int a[100005];
ofile.open ("threesData.bin");
while(ofile>>x)
{
a[cnt++]=x;
}
//call mergesort
mergesort(a, 0, cnt - 1);
ofstream opfile;//for output file
opfile.open("sortedThreesData.bin");
for(int i=0;i
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started