简体   繁体   English

找到最坏的情况

[英]find the worst-case

for(i=0;i<N;i++){
  sequence of statements 
 }
for(j=0;j<M;j++){
  sequence of statements
 }

how would the complexity change if the second loop went to N instead of M?如果第二个循环转到 N 而不是 M,复杂性会如何变化?

tl;dr : O(N) . tl;博士O(N)


Instead of O(N + M) it would then be O(2 * N) which is the same complexity class as O(N) .而不是O(N + M)它将是O(2 * N) ,它是与O(N)相同的复杂度类。

Of course under the assumption that "sequence of statements" run in constant time.当然,假设“语句序列”在恒定时间内运行。

If the first loop runs N times and Second M times it would be o(N+M) , and if the second loop runs N then it would be o(N+N) or o(2N) which means o(N).如果第一个循环运行 N 次,第二个循环运行 M 次,则为 o(N+M) ,如果第二个循环运行 N 次,则为 o(N+N) 或 o(2N) ,即 o(N)。 (Independent loops run in constant time) (独立循环以恒定时间运行)

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

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