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
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
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