Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

My startTime and endTime variables return the same number in my main() code when I run the Merge Sort algorithm (which also causes my totalTime

My "startTime" and "endTime" variables return the same number in my main() code when I run the Merge Sort algorithm (which also causes my totalTime to equal 0). The code only works when n>1000. I tried running the code with the high frequency clock, but it wouldn't compile because I do not have the C++ 11 compiler.

int main() { clock_t startTime; clock_t endTime; clock_t totalTime; int n, i; for(int h = 0; h < 7; h++) { cout << " Enter the number of data elements to be sorted: "; cin >> n; int arr[n]; for(i = 0; i < n; i++) { arr[i] = i+1; } //random permutation of the array random_shuffle(&arr[0],&arr[n]); startTime = clock(); MergeSort(arr, 0, n-1); endTime = clock(); cout << endl << (float)startTime << endl; cout << endl << (float)endTime << endl; totalTime = (endTime-startTime); cout << endl << (float)totalTime*1000000 / CLOCKS_PER_SEC << endl; /* // Printing the sorted data. cout<<" Sorted Data "; for (i = 0; i < n; i++) { cout << " -> " << arr[i]; } */ } return 0; //Output: //Enter the number of data elements to be sorted: 500 //3963 //3963 //0 }

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

Step: 3

blur-text-image

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

Building Database Driven Catalogs

Authors: Sherif Danish

1st Edition

0070153078, 978-0070153073

More Books

Students also viewed these Databases questions