Question: 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
Get step-by-step solutions from verified subject matter experts
