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?
tl;dr : O(N)
.
Instead of O(N + M)
it would then be O(2 * N)
which is the same complexity class as 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). (Independent loops run in constant time)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.