Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Please expert to help me out! Thanks! Code the merge sort in C++. Make sure to base your code on the pseudocode in the book

Please expert to help me out! Thanks! Code the merge sort in C++. Make sure to base your code on the pseudocode in the book (not on things that you find on the internet). Create 2 .cpp files: sortMain.cpp and mergeSort.cpp. Below is a template that you should use:

 //---------------------------------------------------------------------- //file: sortMain.cpp #include  #include  extern void mergeSort ( int A[], int n ); using namespace std; int main ( int argc, char* argv[] ) { . . . } //---------------------------------------------------------------------- //file: mergeSort.cpp void mergeSort ( int A[], int n ) { . . . } //you are free to define other functions of your own. 

Run the sort on input sizes of 10, 100, 1000, 10000, 100000, 200000, 300000, 400000, 500000, and 1000000 of random values. Depending upon the speed of your computer, you may only be able to run one or a few sorts for larger input sizes. Report the array size n (N), number of test iterations (#), total elapsed time (tElapsed), total CPU time (tCPU), average CPU time (avgCPU) for insertion sort in a table like the following:

merge sort

N # tElapsed tCPU avgCPU
10
100
1000
10000

Below is the code i had:

//File: mergeSort.cpp #include using namespace std;

// Merges two subarrays of arr[]. // First subarray is arr[l..m] // Second subarray is arr[m+1..r] void merge(int arr[], int l, int m, int r) { int n1 = m - l + 1; int n2 = r - m;

// Create temp arrays int* L = new int[n1]; int* R = new int[n2];

// Copy data to temp arrays L[] and R[] for (int i = 0; i < n1; i++) L[i] = arr[l + i]; for (int j = 0; j < n2; j++) R[j] = arr[m + 1 + j];

// Merge the temp arrays back into arr[l..r]

int i = 0;// Initial index of first subarray

int j = 0;// Initial index of second subarray

// Initial index of merged subarray int k = l;

while (i < n1 && j < n2) { if (L[i] <= R[j]) { arr[k] = L[i]; i++; } else { arr[k] = R[j]; j++; } k++; }

// Copy the remaining elements of // L[], if there are any while (i < n1) { arr[k] = L[i]; i++; k++; }

// Copy the remaining elements of // R[], if there are any while (j < n2) { arr[k] = R[j]; j++; k++; } }

// l is for left index and r is // right index of the sub-array // of arr to be sorted */ void mergeSort(int arr[], int l, int r) { if (l >= r) { return;//returns recursively } int m = l + (r - l) / 2; mergeSort(arr, l, m); mergeSort(arr, m + 1, r); merge(arr, l, m, r); }

// UTILITY FUNCTIONS // Function to print an array void printArray(int A[], int size) { for (int i = 0; i < size; i++) cout << A[i] << " "; }

-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

//File: sortMain.cpp #include #include #include #include #include

extern void mergeSort(int A[], int n, int m);

using namespace std;

static double cpuTime(void) { FILETIME createTime, exitTime, kernelTime, userTime;

if (GetProcessTimes(GetCurrentProcess(), &createTime, &exitTime, &kernelTime, &userTime) != -1) { SYSTEMTIME userSystemTime; if (FileTimeToSystemTime(&userTime, &userSystemTime) != -1) return (double)userSystemTime.wHour * 3600.0 + (double)userSystemTime.wMinute * 60.0 + (double)userSystemTime.wSecond + (double)userSystemTime.wMilliseconds / 1000.0; } return -1; }

int main(int argc, char* argv[]) {

// int N[] = { 10, 100, 1000, 10000, 100000, 200000, 300000, 400000, 500000, 1000000 }; mt19937 gen((unsigned int)time(NULL)); //Standard mersenne_twister_engine seeded with time uniform_int_distribution<> distrib(1, 10000); for (int i = 0; i < 10; ++i) { //Use `distrib` to transform the random unsigned int generated by gen into an int in [1, 6] cout << distrib(gen) << endl; }

int N[10] = {}; mergeSort(N, 0, 5);

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Essential SQLAlchemy Mapping Python To Databases

Authors: Myers, Jason Myers

2nd Edition

1491916567, 9781491916568

More Books

Students also viewed these Databases questions