简体   繁体   English

最坏情况下算法的运行时间

[英]Running time of algorithm in worst case

What's the running time of the following algorithm in the worst-case, assuming that it takes a constant time c1 to do a comparison and another constant time c2 to swap two elements? 在最坏的情况下,假设要花费一个恒定的时间c1进行比较,并且花费另一个恒定的时间c2来交换两个元素,那么以下算法在最坏情况下的运行时间是多少?

for (int i = 0; i < n; i++)
{
    for (int j = 0; j < n - 1; j++)
    {
        if (array[j] > array [j+1])
        {
            swap(array[j], array[j+1]);
        }
    }
}

I get 2+4n^2. 我得到2 + 4n ^ 2。 How I calculate it (starting from the inner loop): 我如何计算(从内部循环开始):

The inner loop runs (n-1) times. 内循环运行(n-1)次。
The first time it runs, there is the initialisation of j and the comparison of j with (n-1) to know whether to enter the loop. 第一次运行时,将进行j的初始化以及j与(n-1)的比较以了解是否进入循环。 This gives 2 instructions. 这给出了2条指令。
Each time it runs, there is the comparison of j with (n-1) to know whether to continue the loop, the increment of j, the array comparison and the swapping. 每次运行时,都会将j与(n-1)进行比较,以了解是否继续循环,j的增量,数组比较和交换。 This gives 4 instructions which run (n-1) times, therefore 4(n-1) instructions. 这给出了运行(n-1)次的4条指令,因此有4(n-1)条指令。 The inner loop thus contains 2+4(n-1) instructions. 因此,内部循环包含2 + 4(n-1)条指令。

The outer loop runs n times. 外循环运行n次。
The first time the outer loop runs, there is the initialisation of i and the comparison of i with n. 外循环第一次运行时,存在i的初始化以及i与n的比较。 This gives 2 instructions. 这给出了2条指令。
Each time it runs, there is the comparison of i with n, the increment of i and the inner loop. 每次运行时,都会将i与n进行比较,i的增量和内部循环。 This gives (2+(2+4(n-1)))n instructions. 这给出(2+(2 + 4(n-1)))n条指令。

Altogether, there are 2+(2+(2+4(n-1)))n instructions, which gives 2+4n^2. 总共有2+(2+(2 + 4(n-1)))n条指令,即2 + 4n ^ 2。

Is it correct? 这是正确的吗?

You forgot to account for the addition of j+1 for the index in the if statement and the swap call, and the n-1 calculation in the inner for loop will be an extra instruction. 您忘记了在if语句和swap调用中为索引添加j+1 ,内部for循环中的n-1计算将是额外的指令。

Remember, every calculation counts as an instruction, which means that essentially every operator in your code adds an instruction, not just the comparisons, function calls, and loop control stuff. 请记住,每个计算都算作一条指令,这意味着基本上代码中的每个运算符都会添加一条指令,而不仅仅是比较,函数调用和循环控件。

for (int i = 0; i < n; i++)               //(1 + 1) + n(1 + 1 + innerCost)          (init+comp) + numLoops(comp+inc+innerCost)
{
    for (int j = 0; j < n - 1; j++)       //(1 + 2) + (n-1)(1 + 1 + 1 + inner)      (init+comp) + numLoops(sub+comp+inc+innerCost)
    {
        if (array[j] > array [j+1])       //1 + 1        (1 for comparison, 1 for +)
        {
            swap(array[j], array[j+1]);   //1 + 1        (1 for function call, 1 for +)
        }
    }
}

runtime = (1+1) + n(1+1+ (1+2)+(n-1)(1+1+1+ (1+1 + 1+1))) 运行时间= (1+1) + n(1+1+ (1+2)+(n-1)(1+1+1+ (1+1 + 1+1)))

runtime = 2 + n( 2 + 3 +(n-1)( 3 + 2 + 2)) 运行时间= 2 + n( 2 + 3 +(n-1)( 3 + 2 + 2))

runtime = 2 + n( 5 +(n-1)(7)) 运行时间= 2 + n( 5 +(n-1)(7))

runtime = 2 + n( 5 + 7n - 7) 运行时间= 2 + n( 5 + 7n - 7)

runtime = 2 + n(7n-2) 运行时间= 2 + n(7n-2)

runtime = 2 + 7n^2 - 2n = 7n^2 - 2n + 2 运行时间= 2 + 7n^2 - 2n = 7n^2 - 2n + 2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM