简体   繁体   English

if语句的Big-O表示法?

[英]Big-O notation for if statements?

I was wondering what the Big O notation for this would be. 我想知道Big O表示什么。 I know the for loop is O(n). 我知道for循环是O(n)。 I wasn't sure if the if statements were O(n log n). 我不确定if语句是否为O(n log n)。 If so, doesn't that make the run time complexity (n)*((n log n)^3). 如果是这样,这是否会使运行时复杂度(n)*((n log n)^ 3)。 Or would it be ((n^2)(log^3n)) ? 还是((n ^ 2)(log ^ 3n))? Also I know storage in an array is O(n) and was wondering if calling elements in a the same array is O(n) or had a different run tim complexity. 我也知道数组中的存储为O(n),并且想知道同一数组中的调用元素是否为O(n)或运行时间复杂度是否不同。 (Written in Java eclipse) (用Java Eclipse写)

    for (i=0;i<numberOfProblems;i++){                       
    String string1= ap.nextString("P or NP?");
    if(string1=P){
        pOrNPValue[i]=0;
    }else{
        pOrNPValue[i]=1;


        String string2 = ap.nextString("Best Case Run Time?");

        if(string2==ok){
            bestCaseValue[i]=0;
        }else if(string2=oLogLogN){
            bestCaseValue[i]=1;
        } else if(string2=oLogN){
            bestCaseValue[i]=2;
        }else if(string2=oNC){
            bestCaseValue[i]=3;
        }else if(string2=oN){
            bestCaseValue[i]=4;
        }else if(string2=oNLogStarN){
            bestCaseValue[i]=5;
        }else if(string2=oNLogN){
            bestCaseValue[i]=6;
        }else if(string2=oNK){
            bestCaseValue[i]=7;
        }else if(string2=oCN){
            bestCaseValue[i]=8;
        }else if(string2=oNFactorial){
            bestCaseValue[i]=9;
        }

        String string3 = ap.nextString("Average Case Run Time?");

        if(string3=ok){
            averageCaseValue[i]=0;
        }else if(string3=oLogLogN){
            averageCaseValue[i]=1;
        } else if(string3=oLogN){
            averageCaseValue[i]=2;
        }else if(string3=oNC){
            averageCaseValue[i]=3;
        }else if(string3=oN){
            averageCaseValue[i]=4;
        }else if(string3=oNLogStarN){
            averageCaseValue[i]=5;
        }else if(string3=oLogLogN){
            averageCaseValue[i]=6;
        }else if(string3=oNK){
            averageCaseValue[i]=7;
        }else if(string3=oCN){
            averageCaseValue[i]=8;
        }else if(string3=oNFactorial){
            averageCaseValue[i]=9;
        }

        String string4 = ap.nextString("Worst Case Run Time?");

        if(string4=ok){
            worstCaseValue[i]=0;
        }else if(string4=oLogLogN){
            worstCaseValue[i]=1;
        } else if(string4=oLogN){
            worstCaseValue[i]=2;
        }else if(string4=oNC){
            worstCaseValue[i]=3;
        }else if(string4=oN){
            worstCaseValue[i]=4;
        }else if(string3=oNLogStarN){
            worstCaseValue[i]=5;
        }else if(string4=oLogLogN){
            worstCaseValue[i]=6;
        }else if(string4=oNK){
            worstCaseValue[i]=7;
        }else if(string4=oCN){
            worstCaseValue[i]=8;
        }else if(string4=oNFactorial){
            worstCaseValue[i]=9;

String comparisons take time proportional to the length of the longer of the two strings (you only need to compare characters up to that point in the worst-case). 字符串比较所花费的时间与两个字符串中较长者的长度成正比(在最坏的情况下,您只需要比较直到该点的字符)。 Since all of the string comparisons that are being made here are against constant strings, each comparison individually takes time O(1). 由于此处要进行的所有字符串比较都是针对常量字符串,因此每个比较分别花费时间O(1)。 Since there's only a fixed number of comparisons, each of which does O(1) work if true (array accesses take time O(1) regardless of the index), the total time required for all the comparisons is O(1) per iteration. 由于只有固定数量的比较,如果为true,则每个O(1)都会起作用(与索引无关,数组访问花费的时间为O(1)),因此每次迭代的所有比较所需的总时间为O(1) 。 Thus the total amount of work done is O(n): there are O(n) loop iterations, and each of them does O(1) work. 因此,完成的工作总量为O(n):存在O(n)个循环迭代,并且每个迭代都进行O(1)个工作。

(Technically speaking, you need to account for the work done reading characters from the user, which could be unbounded because the user could just hold down a key indefinitely. I'll ignore that for now by assuming there's a fixed limit to the total number of characters the user can type at each prompt.) (从技术上讲,您需要考虑从用户读取字符完成的工作,这可能是无限制的,因为用户可以无限期地按住某个键。我现在假设总数目有一个固定的限制,而现在将忽略这一点。用户可以在每个提示符下键入的字符数。)

In general, comparisons themselves take O(1) time and the real question is how much work is required to evaluate the boolean expression. 通常,比较本身需要O(1)的时间,真正的问题是评估布尔表达式需要多少工作。

Hope this helps! 希望这可以帮助!

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

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