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

opfile<

opfile.close();

ofile.close();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!