c++ sorting algorithms duration
algorithm, c++, duration, sorting, time
Solution
The error in your program is that you reset duration throughout the loop. A cleaner way to handle the time would be to put the `duration` variable modification outside the for loop. For example:
duration = clock();
for(int i = 0; i < 2000; i++){
a2 = a4;
mergeSort(a2, 0, N - 1);
}
duration = clock() - duration
EDIT: forgot to remove the part inside the loop. Fixed now.
Problem
I have been working on calculating duration of these sorting algorithms take. I looped all sorting methods 2000 times then divide total duration into 2000 to get a proper value for duration. The problem is; it does not show the exact value of time that the particular code parts of sorting methods take. I mean the `duration` variable shows increasing values through the program flows. For example, for `N = 10000`, `insertionSort()`gives 0.000635, `mergeSort()`gives 0.00836 and `heapSort()` gives 0.018485 and when I change order of these, `duration` still goes up through program, regardless of algorithm type. I tried giving different duration values for each process but that didn't work. Can someone help me to understand this problem or are there any other time measuring styles? Sorry if this is a dumb problem and for my bad grammar. ``` int main(){ srand(time(NULL)); int N, duration; cout << endl << "N : "; cin >> N; // N is array sze. cout << endl; // a4 would be the buffer array (for calculating proper duration). int *a1 = new int[N]; int *a2 = new int[N]; int *a3 = new int[N]; int *a4 = new int[N]; cout << endl << "Unsorted array : " << endl; for (int i = 0; i < N; i++){ a4[i] = rand() % 100; cout << a4[i] << " "; } /*------------------------------------------------------------------------------*/ cout << endl << endl <<"Sorting with Insertion Sort, please wait..." << endl; for(int i = 0; i < 2000; i++){ a1 = a4; duration = clock(); insertionSort(a1, N - 1); duration += clock() - duration; } cout << endl << "Insertion sort : " << endl; print(a1, N); cout << endl << endl << "Approximate duration for Insertion Sort : "; cout << (double) (duration / 2000) / CLOCKS_PER_SEC; cout << " s." << endl; /*------------------------------------------------------------------------------*/ cout << endl << endl << "Sorting with Merge Sort, please wait..." << endl; for(int i = 0; i < 2000; i++){ a2 = a4; duration = clock(); mergeSort(a2, 0, N - 1); duration += clock() - duration; } cout << endl << "Merge sort : " << endl; print(a2, N); cout << endl << endl << "Approximate duration for Merge Sort : "; cout << (double) (duration / 2000) / CLOCKS_PER_SEC; cout << " s."<< endl << endl; /*------------------------------------------------------------------------------*/ cout << endl << endl << "Sorting with Heap Sort, please wait..." << endl; for(int i = 0; i < 2000; i++){ a3 = a4; duration = clock(); heapSort(a3, N); duration += clock() - duration; } cout << endl << "Heap sort : " << endl; print(a3, N); cout << endl << endl << "Approximate duration for Heap Sort : "; cout << (double) (duration / 2000) / CLOCKS_PER_SEC; cout << " s."<< endl << endl; return 0; } ```