What is time complexity for the following code?

后端 未结 5 1085
刺人心
刺人心 2021-01-17 06:33

It seems the complexity of the following code should be O(n^2) but it\'s O(n), how?

void fun(int n, int arr[])
{
    int i = 0, j = 0;
    for(; i < n; ++         


        
5条回答
  •  鱼传尺愫
    2021-01-17 07:03

    The answer is O(n) because the test condition inside the 'while' loop fails!

    while(j < n && arr[i] < arr[j])
    

    In the beginning, i=0 and j=0, which means arr[i] = arr[j], but the while loop test condition says arr[i], and its completely wrong to assume arr[0]

    The code only runs the for loop n times.

    The final answer is O(n) not O(n^2)

    For some clarity, you can go through these two examples

    Example 1 :

    #include 
    
    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;
    }
    

    The Output is :

    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
    

    In the above output, we can't find the print statement present in 'while' loop

    Example 2 :

    #include 
    
        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;
        }
    

    Here, a small change is made arr[i] <= arr[j]

    '=' is used

    Output:

    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
    

    Here, the print statement in while loop is executed, because arr[i]=arr[j], arr[0] = arr[0]

    For the 'Example 2' shown above the time complexity is O(n^2)

提交回复
热议问题