简体   繁体   English

计算此方法的效率

[英]Figuring out the efficiency of this method

I have come across while developing my program in Java. I need to figure out how efficient this method is in Big-O notation.我在 Java 开发我的程序时遇到过。我需要弄清楚这种方法在 Big-O 表示法中的效率。

This is what my boss told me:这是我的老板告诉我的:

Making use of a suitable Big-O expression, state and explain in some detail the time complexity of method compute.使用合适的 Big-O 表达式 state 并详细解释方法计算的时间复杂度。 Your explanation must include a justification for the chosen Big-O expression and make reference to the number of calls to compute at Line 6.您的解释必须包含所选 Big-O 表达式的理由,并参考第 6 行的计算调用次数。

 public int compute(int[] array, int first, int last) { int result = 0; if(first < last) { result = array[first] + compute(array, first+2, last); } else if(first < array.length) { result = array[first]; } return result; }

To answer problems like this, it's useful to walk through the code yourself and see what it does.要回答此类问题,亲自浏览代码并查看其功能很有用。 Once you can put the method into regular words, it usually becomes much easier to figure out the efficiency.一旦你能把这个方法用常规的词表达出来,通常就更容易计算出效率了。

What does this method do, then?那么这个方法有什么作用呢? It adds together every other number in the array from first to last .它将数组中从firstlast的所有其他数字相加。 Examining it closer, we can break the function down into仔细检查,我们可以将 function 分解为

  • The base cases基本案例
    • else if (first < array.length) {... } returns the element at index first , or the constant 0 . else if (first < array.length) {... }返回索引first处的元素,或常量0 Since array-indexing is a constant-time operation, this branch executes in constant time.由于数组索引是恒定时间操作,因此该分支在恒定时间内执行。
    • Implicitly, result remains 0 if neither of the above cases are triggered.隐含地,如果上述两种情况均未触发,则result仍为 0。 This is obviously constant-time.这显然是恒定时间。
  • The recursive case if (first < last) .递归情况if (first < last) In this case, we end up returning the array[first] added to the recursive result of calling the function with first+2 .在这种情况下,我们最终返回array[first]添加到使用first+2调用 function 的递归结果。

Looking at the function, we can see that with each iteration first gets closer by 2 to last , until they touch, at which point the function returns.查看 function,我们可以看到每次迭代first都更接近last 2,直到它们接触,此时 function 返回。

So, how many times does this happen?那么,这种情况会发生多少次? The answer is (last - first) / 2 .答案是(last - first) / 2 If first always starts at 0, and last always starts at the last index of the array, then the number of executions will scale linearly with the length of the array .如果first总是从 0 开始, last总是从数组的最后一个索引开始,那么执行次数将与array的长度成线性比例。 Let's call {the length of the array} n .让我们调用 {数组的长度} n

The function, thus, executes a constant number of operations O(1) , and is called O( n ) times.因此,function 执行常数O(1)次操作,并被调用 O( n ) 次。 O(1) * O(n) == O(n) . O(1) * O(n) == O(n)

I would assume that you can shorten all of this to about 2-3 lines using expression operators.我假设您可以使用表达式运算符将所有这些缩短到大约 2-3 行。 You can use the ?: operator to determine the value of result , and then return the result.您可以使用?:运算符确定result的值,然后返回结果。

?: is used as a sort of if/else statement, so that's why it makes it better. ?:被用作一种 if/else 语句,所以这就是它让它变得更好的原因。 I'm not sure if I'm mixing up efficiency with readability, though.不过,我不确定我是否将效率与可读性混为一谈。

Here's the example:这是示例:

public int compute(int[] array, int first, int last) {
        int result = 0;
        
        result = first < last ? array[first] + compute(array, first+2, last) : first < array.length ? array[first] : result;
        
        return result;
    }```

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

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