Question: I have this c++ mergesort function , to sort 100000 random integers. I need to find the time to run and number of comparisons. I

I have this c++ mergesort function , to sort 100000 random integers. I need to find the time to run and number of comparisons. I got the time but I do not know where would be the best place to set a counter of comparison on mergesort, where would this be placed? Below is a sample output and my code so far.

mergesort: 16.998 ms comps: 1536383

#include #include #include #include #include

using namespace std; const long int size = 100000; const long int MIN= 1; const long int MAX= 1000000000;

void merge(long int *arr,long int size,long int first,long int middle,long int last){

long int temp[size]; for(int i = first; i<=last; i++) { temp[i] = arr[i]; } int i=first, j=middle+1, k= first; while(i<=middle && j<=last) { if(temp[i] <= temp[j]) { arr[k] = temp[i]; i++; } else { arr[k]=temp[j]; j++; } k++; } while(i<=middle) { arr[k]=temp[i]; k++; i++; } }

void mergesort(long int *arr, long int size,long int first,long int last){ if(first

int main(){ double t1, t2; long int arr[size]; srand(( int)time(0)); // initialize the random number generator for (long int i=0; i

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!