简体   繁体   English

如何计算这个简单算法的最坏情况运行时间复杂度

[英]How to work out worst-case run time complexity of this simple algorithm

I am trying to learn how to work out worst case run time complexity and I was wondering if someone could explain in detail how to work it out for the code below by highlighting all the operations etc.... Sorry for the awful code example, I'm very aware it is the most pointless method ever!我正在尝试学习如何解决最坏情况下的运行时间复杂性,我想知道是否有人可以通过突出显示所有操作等来详细解释如何为下面的代码解决这个问题......对不起,糟糕的代码示例,我很清楚这是有史以来最无意义的方法!

public int two(int[] n){
    int twosFound=0;
    int other=0;

    for (int i = 0; i < n.length; i++) {

        if (n[i]==2) {
            System.out.println("2 is found");
            twosFound++;
        }

        else other++;

    }

    return twosFound;
}

Start with the basic building blocks, and then work your way upwards:从基本的构建块开始,然后向上工作:

  • initializing an int variable does not depend on the array length, therefore it is O(1).初始化int变量不依赖于数组长度,因此它是 O(1)。
  • (By the way, it is a terrible idea to call the array n when you want to talk about O(n). This can easily lead to confusion.) (顺便说一句,当你想谈论 O(n) 时调用数组n是一个糟糕的主意。这很容易导致混淆。)
  • Accessing n.length takes constant time.访问n.length需要恒定的时间。 On the other hand, if n were a linked list or some other data structure, you would have to further analyze it.另一方面,如果n是链表或其他一些数据结构,则必须进一步分析它。
  • Accessing n[i] takes constant time.访问n[i]需要恒定的时间。
  • System.out.println takes constant time, since it neither depends on the array length nor on the array contents. System.out.println花费恒定时间,因为它既不取决于数组长度也不取决于数组内容。
  • Incrementing an int variable takes constant time.增加一个int变量需要恒定的时间。
  • The if statement takes the worst of whatever its components take; if语句采用其组件中最糟糕的部分; in this case it is constant time.在这种情况下,它是恒定时间。
  • The for loop iterates linearly over the array, therefore it takes O(n.length), multiplied with O( whatever happens inside the body of the for statement ). for循环在数组上线性迭代,因此它需要 O(n.length),乘以 O( for语句主体内发生的任何事情)。
  • In this case, this is O(n) * O(1) = O(n).在这种情况下,这是 O(n) * O(1) = O(n)。

The time complexity if O(n) (actually, exactly n ) because control accesses all n elements of the array.时间复杂度为 O(n)(实际上正好是n ),因为控制访问数组的所有 n 个元素。 (plus there are no extra conditions to terminate the loop) (另外没有额外的条件来终止循环)

Your worst case complexity does not differs from the other cases.您最坏情况的复杂性与其他情况没有区别。 Its because of your algorithm.这是因为你的算法。 Its running time is solely dependent on the number of elements in the array.它的运行时间完全取决于数组中元素的数量。 You test each element to satisfy a condition, the more elements you have the longer it will take the algo to run.您测试每个元素以满足一个条件,您拥有的元素越多,算法运行所需的时间就越长。

In fact it clear that time complexity is O(number_of_elements) .事实上很明显,时间复杂度是O(number_of_elements) It means that time is linear dependent on the number of elements.这意味着时间与元素数量呈线性关系。 If you take twice bigger array the time will also increase twice.如果您采用两倍大的阵列,时间也会增加两倍。

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

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