Given the following bit of code, how would I calculate the complexity?
for (a = 0; a < i; a++) {
//some code
//some code
for (b = 0; b < j; b++) {
//code
for (c = 0; c < k; c++) {
//code
}
}
}
Assuming the commented lines have complexity of 1, would the complexity be calculated as follows?
a loop happens $i$ times
b loop happens $i^2j$ times
c loop happens $i^3j^2k$ times
Complexity = $2i + i^2j + i^3j^2k$?
Start with the inner
cloop. Suppose you add a counter statementctr++;to it. Because there loop count isk, one completecloop addskto the counter and therefore one pass through thebloop addskto the counter: there arejpasses of thebloop and thus a total increment ofj*k. Repeat the argument for theipasses of thealoop and you get an overall count ofi*j*kfor your whole code snippet.