Number one, you do not see reset duration between runs of different varieties. This means that the sum of the individual iteration durations will propagate down through each sorting phase (unless the next point was also a problem).
Then you need to set up a separate variable, call its durationSum and use it when you are currently using duration in the summary phase after iteration. You are currently deleting your amount at each iteration.
For instance:
clock_t durationSum = 0; clock_t duration = 0; for(int i = 0; i < 2000; i++){ a1 = a4; duration = clock(); insertionSort(a1, N - 1); durationSum += clock() - duration; }
Then you make a type error when amortizing duration . You have:
cout << (double) (duration / 2000) / CLOCKS_PER_SEC;
With minimal changes, this will work more accurately (but should use durationSum ):
cout << (double) (duration / 2000.0) / CLOCKS_PER_SEC;
You used to say "use integer division to divide duration by 2000, THEN advance it to double and divide by CLOCKS_PER_SEC (this time with floating point division, since one of the operands is double and one integral). Using 2000.0 , the duration will be upgraded to double for floating point division by 2000.
Finally, it would be better to consider the loop overhead that is negligible compared to a single iteration of the sort, and only make two calls to the clock () function in 2000 sort sorts.
For instance:
clock_t insert_sort_start = clock(); for(int i = 0; i < 2000; i++){ a1 = a4; insertionSort(a1, N - 1); } double duration_sec = (clock() - insert_sort_start) / 2000.0 / CLOCKS_PER_SEC;
Finally, note that you use duration as an int , whereas in fact it is clock_t , and if you are on a 64-bit system, it is very likely that this is the 64-bit number returned by clock() and βnarrowedβ (downcast) into a 32 bit integer int . Use clock_t .