Prashnikaप्रश्निका
Code
About
‹ Back to the paper

Question 2(d)(i)

1 mark

Computer ScienceISC 2017 Computer Science Paper 1Short answerComplexity and Big O notation

Answer the following question.

What is the worst case complexity of the code segment?

Show the code
for (int x = 1; x <= a; x++)
{
    statements;
}
for (int y = 1; y <= b; y++)
{
    for (int z = 1; z <= c; z++)
    {
        statements;
    }
}

Answer

Answer

$O(a + b \times c)$ The first loop runs a times and the nested loops run b x c times; the two segments are sequential, so their costs add.