Time complexity 以下代码的时间复杂度是多少?

Time complexity 以下代码的时间复杂度是多少?,time-complexity,Time Complexity,下面代码的复杂度似乎应该是O(n^2),但它是O(n),如何 void fun(int n,int arr[]) { int i=0,j=0; 对于(;i而循环在最坏的情况下再次执行,即j=2。 ... 第n次迭代:i=n-1,j=n-2=>而循环将在最坏的情况下再次执行,即j=n-1 因此,通过做这个练习,我们可以观察到每次j=i-1 除了i=0和j=0之外,或者我们可以说while循环只是与for循环并行运行,因此while循环的执行次数等于for循环的执行次数。 因此,阶数=O(n) 在第

下面代码的复杂度似乎应该是O(n^2),但它是O(n),如何

void fun(int n,int arr[])
{
int i=0,j=0;
对于(;i
j
不会在每次外循环迭代时重置为
0
。因此,它只运行到
n-1
一次,与
i
一样。因此,您有两个并行/混合迭代,从
0
到(最多)
n-1


在每一步中,程序将
i
增加1。当
i
达到
n
时,程序终止。“外循环”运行
n次

还有一个关于
j
的“内环”。但它所做的只是增加
j
,直到达到
i
(最多,有时做得更少)<代码>j
永远不会减少。因此,该部分也最多运行
n
次(而不是“外循环”的每次迭代运行
n
次)。

答案是O(n) 外部循环运行“n”次,而内部循环在所有迭代组合中仅运行“n”次,因为j的值从未重置为0。
因此,答案是O(n+n)=o(n)。

让我们考虑当执行while循环次数最多的情况下最坏的情况。 最初:
i=0
j=0
=>而循环不会执行,因为
arr[0]=arr[0]
j=0
。 第二次迭代:
i=1
j=0
=>而循环在最坏的情况下执行,即
j=1
。 第三次迭代:
i=2
j=1
=>而循环在最坏的情况下再次执行,即
j=2
。 ... 第n次迭代:
i=n-1
j=n-2
=>而循环将在最坏的情况下再次执行,即
j=n-1

因此,通过做这个练习,我们可以观察到每次
j=i-1
除了
i=0
j=0
之外,或者我们可以说while循环只是与for循环并行运行,因此while循环的执行次数等于for循环的执行次数。
因此,阶数=O(n)

在第一次查看中,由于两个循环,时间复杂度似乎为O(n^2)。但是,请注意,变量j不是针对变量i的每个值初始化的

因此,内部j++最多执行n次

i循环也运行n次

所以,整个过程运行了O(n)次

请注意上述功能与以下功能之间的差异:

void fun(int n, int arr[])
               {
int i = 0, j = 0;
for(; i < n; ++i)
{
    j = 0;
    while(j < n && arr[i] < arr[j])
        j++;
}
void fun(int n,int arr[])
{
int i=0,j=0;
对于(;i
}`

还是不相信吗?

让我们假设传递的数组的元素顺序是递减的。我们将只对代码进行干运行:

               Iteration 1 : i = 0, j = 0. arr[0] < arr[0] is false. So, the 
                             inner while loop breaks.
               Iteration 2: i =1, j = 0. arr[1] < arr[0] is true. j becomes 
               Iteration 3 : i = 1, j = 1. Condition false. We break. Note 
                            that j will remain 1 and is not reset back to 0.
               Iteration 4 : i = 2, j = 1. arr[2] < arr[1]. True. j = 2.
               Iteration 5 : i = 2, j = 2. Condition false. Break.
               Iteration 6 : i = 3, j = 2. arr[3] < arr[2]. True. j = 3.
               Iteration 7 : i = 3, j = 3. Condition false. Break.
迭代1:i=0,j=0。arr[0]
如您所见,在这种情况下,内部while循环只运行一次。 因此,总迭代次数是2*N.

答案是O(N),因为“while”循环中的测试条件失败了

while(j < n && arr[i] < arr[j])
输出为:

This is for loop, its running 5 times

This is for loop, its running 5 times

This is for loop, its running 5 times

This is for loop, its running 5 times

This is for loop, its running 5 times
在上面的输出中,我们找不到“while”循环中存在的print语句

例2:

#include <stdio.h>

    int main()
    {
        int i = 0, j = 0; 
        int n = 5;
        int arr[] = {6,7,8,9,10,11};
        for(; i < n; ++i) 
        { 
            printf("\nThis is for loop, its running 5 times\n");
            
            while(j < n && arr[i] <= arr[j]){ 
                j++;
                printf("\nThis is while loop!\n");
            }
        };
        return 0;
    }
这里执行while循环中的print语句,因为
arr[i]=arr[j]
, arr[0]=arr[0]


对于上面显示的“示例2”,时间复杂度为
O(n^2)

请注意,变量j未针对变量i的每个值进行初始化。 因此,内部j++最多执行n次。 i循环也运行n次。
所以,整个事情运行O(n)次。也考虑这个代码空隙乐趣(int n,int ARR[]){int i=0,j=0;对于(i;n;++i){j=0;而(j O(n ^ 2)< /> >。
This is for loop, its running 5 times

This is for loop, its running 5 times

This is for loop, its running 5 times

This is for loop, its running 5 times

This is for loop, its running 5 times
#include <stdio.h>

    int main()
    {
        int i = 0, j = 0; 
        int n = 5;
        int arr[] = {6,7,8,9,10,11};
        for(; i < n; ++i) 
        { 
            printf("\nThis is for loop, its running 5 times\n");
            
            while(j < n && arr[i] <= arr[j]){ 
                j++;
                printf("\nThis is while loop!\n");
            }
        };
        return 0;
    }
This is for loop, its running 5 times                                                                                                                                          
                                                                                                                                                                               
This is while loop!                                                                                                                                                            
                                                                                                                                                                               
This is while loop!                                                                                                                                                            
                                                                                                                                                                               
This is while loop!                                                                                                                                                            
                                                                                                                                                                               
This is while loop!                                                                                                                                                            
                                                                                                                                                                               
This is while loop!                                                                                                                                                            
                                                                                                                                                                               
This is for loop, its running 5 times                                                                                                                                          
                                                                                                                                                                               
This is for loop, its running 5 times                                                                                                                                          
                                                                                                                                                                               
This is for loop, its running 5 times                                                                                                                                          
                                                                                                                                                                               
This is for loop, its running 5 times