计算归并排序的运行次

Calculate the running time of merge sort

当你想计算归并排序的运行时间时,把startTime放在mergeSort函数调用前或调用后是正确的:

    startTime=System.nanoTime();
    mergeSort(temp,0,temp.length - 1);
    endTime=System.nanoTime();
    running-time=(endTime-startTime);

    mergeSort(temp,0,temp.length - 1);
    startTime=System.nanoTime();
    endTime=System.nanoTime();
    duration=(endTime-startTime);

哪个是正确的,因为结果不同?

第一个是正确的,因为它获取操作之前的时间(合并排序),执行操作然后获取操作之后的时间,使用这两个时间来计算所用时间。

第二个不正确,它先运算再计算,计算的时间与运行归并排序算法的时间无关

注意:变量running-time无效。考虑将其更改为 runningTime,或者最坏的情况是 running_time