[英]Algorithm Complexity Analysis Confusion
static int sumAfterPos(int[] A) {
int N = A.length;
for (int i = 0; i < N; i += 1) {
if (A[i] > 0) {
int S;
S = 0;
for (int j = i + 1; j < N; j += 1) {
S += A[j];
}
return S;
}
}
return 0;
}
I am having trouble figuring out whether this code runs in O(n^2) or in O(n). 我在弄清楚此代码是在O(n ^ 2)还是O(n)中运行时遇到麻烦。 I am not sure whether the return S will have a big impact on the runtime.
我不确定返回S是否会对运行时间产生重大影响。
It is O(N)
in time. 时间是
O(N)
。
For example, A[K] > 0
, then you have already have K
steps. 例如,
A[K] > 0
,那么您已经有K
步骤。 Then you run another NK
steps and return. 然后运行另一个
NK
步骤并返回。 So totally you have O(N)
. 因此,总共您有
O(N)
。
Let us say, all A[i] < 0
, this will make the inner loop away. 让我们说,所有
A[i] < 0
,这将使内部循环消失。 So it is O(N)
in this case. 因此,在这种情况下为
O(N)
。
Now let us say, A[0] > 0
, this will make the out loop only repeat once and the inner loop will run from 1 to N - 1, so totally you have 1 + (N-1 - 1 + 1) = N. 现在让我们说
A[0] > 0
,这将使out循环仅重复一次,并且内部循环将从1到N-1运行,因此总共有1 +(N-1-1 + 1)= N.
Now let us say, A[1] > 0
, this will make the out loop only repeat twice and the inner loop will run from 2 to N - 1, so totally you have 2 + (N-1 - 2 + 1) = N. 现在让我们说
A[1] > 0
,这将使out循环仅重复两次,并且内部循环将从2到N-1运行,因此总共有2 +(N-1-2 +1)= N.
... ...
Now let us say, A[k] > 0
, this will make the out loop only repeat k + 1 times and the inner loop will run from k + 1 to N - 1, so totally you have k + 1 + (N-1 - k -1 + 1) = N. 现在让我们说
A[k] > 0
,这将使外循环仅重复k + 1次,而内循环将从k + 1到N-1运行,因此总共有k + 1 +(N- 1-k -1 +1)=N。
Now let us say, A[N-1] > 0
, this will make the out loop only repeat N and the inner loop will never run, so totally you have N times. 现在让我们说
A[N-1] > 0
,这将使out循环仅重复N个,而内部循环将永远不会运行,因此总共有N次。
This looks to be O(n)
, or rather, exactly N
iterations will occur. 这看起来是
O(n)
或更确切地说, 恰好 N
迭代都会发生。 If the statement (A[i] > 0)
is true, then a value would be returned after the inner for
loop completes its iterations. 如果语句
(A[i] > 0)
为true,则在内部for
循环完成其迭代之后将返回一个值。 If there are N
elements in the array A
, and the outer for
loop is at iteration i
, and the conditional is met, then the inner for
loop will iterate at most Ni
times and then return. 如果数组
A
有N
元素,并且外部for
循环在迭代i
,并且满足条件,则内部for
循环将最多迭代Ni
次,然后返回。 If the conditional is never met, then the outer for
loop will iterate N
times. 如果从不满足条件,则外部
for
循环将迭代N
次。 Thus, exactly N
iterations between the outer- and inner- for
loops will execute. 因此,恰好
N
的outer-和inner-之间迭代for
循环将执行。
Return statements are generally never taken into account in determining the runtime complexity of an algorithm (unless the return statement for some reason is non-trivially complex (eg recursion)). 在确定算法的运行时复杂度时,通常不会考虑使用return语句(除非由于某种原因return语句不是很复杂的(例如递归))。
EDIT: For another perspective, note that as the index i
increases linearly, the starting point for the index j
increases linearly at the same rate, and thus the possible remaining work is decreasing linearly at the same rate as well. 编辑:从另一个角度来看,请注意,随着索引
i
线性增加,索引j
的起点以相同的速率线性增加,因此可能的剩余功也以相同的速率线性减少。 Since the the function is guaranteed to return once the inner loop is reached, the total number of iterations between the two for loops is i+j=N
by the end of execution. 由于确保一旦到达内部循环,函数便会返回, 因此在执行结束时, 两个for循环之间的迭代总数为
i+j=N
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.