Your answer 1 is correct, it's inside a loop controlled only by n.
Answer 2 is incorrect. It would be O(log n) if line 7 did not exist but, because line 7 is forcing lines 8 and 9 to run multiple times dependent on n, the answer is O(n log n).
Answer 3 is the correct reasoning but suffers from the fact answer 2 was wrong. O(n) + O(n log n) simplifies down to O(n log n).
我不知道问题是如何提出的,但如果措辞像你说的那样,你的考官不知道大O的正确定义(至少当他期望“正确”答案时)——因为“大O函数包含更小的函数”。因此,在 f(n) = 10 n 中作为 n 函数执行的线性函数也在 O(n)、O(n^2)、O(n log n) 中。 如果有人要求尽可能的“最小”,那么您的答案将是
语句 4 执行了 10 n 次,因此 A
语句 9 执行了 n*log n 次,因此 D
这里执行的是两者的总和,n + n*log n 所以(这里你失去了一个 *n),所以 D 是正确的。
因此,如果有多个答案,并且只是询问执行了多少,则正确答案将是
A,B,D
B,D
B,D
I dont know how the questions where formulated, but if the wording is like you say, your examiner didnt know the right definition of big O (at least when he expects the "right" answers) – as "Big O functions include smaller". So something that executes as a function of n in f(n) = 10 n which is linear is also in O(n), O(n^2), O(n log n). If one asks for the "smallest" possible, your answers would be
Statement 4 is executed 10 n times, so A
Statement 9 is executed n*log n times, so D
Here it is executed the sum of both, n + n*log n so (here you lost an *n), so D would be the right.
So if multiple answers were possible and it was just asked for how much it is executed, the right answers would be
发布评论
评论(3)
您的答案 1 是正确的,它位于仅由
n
控制的循环内。答案2不正确。如果第 7 行不存在,那么将为
O(log n)
,但是,因为第 7 行强制第 8 行和第 9 行依赖于n< /code>,答案是
O(n log n)
。答案 3 是正确的推理,但事实是答案 2 是错误的。
O(n) + O(n log n)
简化为O(n log n)
。所以答案是
A
、D
和D
。Your answer 1 is correct, it's inside a loop controlled only by
n
.Answer 2 is incorrect. It would be
O(log n)
if line 7 did not exist but, because line 7 is forcing lines 8 and 9 to run multiple times dependent onn
, the answer isO(n log n)
.Answer 3 is the correct reasoning but suffers from the fact answer 2 was wrong.
O(n) + O(n log n)
simplifies down toO(n log n)
.So the answers are
A
,D
andD
.我不知道问题是如何提出的,但如果措辞像你说的那样,你的考官不知道大O的正确定义(至少当他期望“正确”答案时)——因为“大O函数包含更小的函数”。因此,在 f(n) = 10 n 中作为 n 函数执行的线性函数也在 O(n)、O(n^2)、O(n log n) 中。
如果有人要求尽可能的“最小”,那么您的答案将是
因此,如果有多个答案,并且只是询问执行了多少,则正确答案将是
I dont know how the questions where formulated, but if the wording is like you say, your examiner didnt know the right definition of big O (at least when he expects the "right" answers) – as "Big O functions include smaller". So something that executes as a function of n in f(n) = 10 n which is linear is also in O(n), O(n^2), O(n log n).
If one asks for the "smallest" possible, your answers would be
So if multiple answers were possible and it was just asked for how much it is executed, the right answers would be
Ans 1: A 即。 O(n),因为语句 4 将执行 10*n 次。
Ans 2: D即。 O(nlog(n)),因为语句 9 将执行 n*log(n) 次。
Ans 3:D 作为整体复杂度 [O(n) + O(nlog(n))] 将为 n*log(n)。
Ans 1: A ie. O(n) as the statement 4 would be executed 10*n times.
Ans 2: D ie. O(nlog(n)) as the statement 9 would be executed n*log(n) times.
Ans 3: D as the overall complexity [O(n) + O(nlog(n))] would be n*log(n).