MCS-211 · Block 1 Unit 3

Complexity Analysis of Simple Algorithms

Section 1 of 8

Block 1 · Unit 3Complexity Analysis of Simple Algorithms7 min · Section 1 of 8

3.1Analysing Simple Constructs

Covers IGNOU MCS-211 §3.2, 3.3 (plus while/repeat constructs from the programme guide)

Every algorithm is built from a few constructs: simple statements, loops and conditionals. If you know the cost of each construct and how costs combine, you can analyse almost any non-recursive algorithm. This page gives those rules.

A quick review of the notations

For functions T(n)T(n) and f(n)f(n) with non-negative values:

NotationMeaningCondition (constants C>0C > 0, n0n_0; all n≥n0n \ge n_0)
T(n)=O(f(n))T(n) = O(f(n))TT grows at most like ffT(n)≤Cf(n)T(n) \le C f(n)
T(n)=Ω(f(n))T(n) = \Omega(f(n))TT grows at least like ffT(n)≥Cf(n)T(n) \ge C f(n)
T(n)=Θ(f(n))T(n) = \Theta(f(n))TT grows like ffboth of the above

For example, 1000n1000n is larger than n3n^3 while n<1000≈31.6n < \sqrt{1000} \approx 31.6, but from n=32n = 32 on n3n^3 is larger; so 1000n=O(n3)1000n = O(n^3), while n3≠O(n)n^3 \ne O(n).

The rules

ConstructRunning timeExample
Sequence of simple statements (no loops, calls or recursion)O(1)O(1)x = x + 5; x = x - 5;
Loop running kk times with an O(1)O(1) bodyO(k)O(k)—
for (i = 1; i <= n; i += c), constant c≥1c \ge 1⌈n/c⌉\lceil n/c \rceil iterations: Θ(n)\Theta(n)step 3, n=100n = 100: 34 iterations
for (i = 1; i <= n; i *= c) or for (i = n; i > 0; i /= c), constant c≥2c \ge 2about log⁡cn\log_c n iterations: Θ(log⁡n)\Theta(\log n)c=3c = 3, n=100n = 100: 5 iterations
Nested loopsproduct of the iteration countstwo loops of nn: Θ(n2)\Theta(n^2)
Consecutive loopssum of the costsmm then nn iterations: O(m+n)O(m + n)
if (cond) S1 else S2cost of the test + the larger of S1S_1, S2S_2 (worst case)—
while / repeat loop(number of iterations) × (cost of one iteration); find the count from how the loop variable changeswhile (k > 1) k = k / 2; runs ⌊log⁡2k⌋\lfloor \log_2 k \rfloor times

The multiplying loop needs c≥2c \ge 2: with c=1c = 1 the variable never changes and the loop never ends.

C · loop_counts.c
#include <stdio.h>

/* Counts how often the loop body runs for the simple constructs. */
int main(void)
{
    int n = 100, c = 3, m = 40;
    int step = 0, mult = 0, divi = 0, nested = 0, consec = 0, wh = 0;

    for (int i = 1; i <= n; i += c)      /* step c: about n/c times   */
        step++;
    for (int i = 1; i <= n; i *= c)      /* multiply by c: log_c n    */
        mult++;
    for (int i = n; i > 0; i /= c)       /* divide by c: log_c n      */
        divi++;
    for (int i = 1; i <= n; i += c)      /* nested: (n/c) * (n/c)     */
        for (int j = 1; j <= n; j += c)
            nested++;
    for (int i = 1; i <= m; i++)         /* consecutive loops: m + n  */
        consec++;
    for (int j = 1; j <= n; j++)
        consec++;
    int k = n;                           /* while loop: halve until 1 */
    while (k > 1) {
        k = k / 2;
        wh++;
    }

    printf("step by %d    : %d\n", c, step);
    printf("multiply by %d: %d\n", c, mult);
    printf("divide by %d  : %d\n", c, divi);
    printf("nested       : %d\n", nested);
    printf("consecutive  : %d\n", consec);
    printf("while halving: %d\n", wh);
    return 0;
}
Output
step by 3    : 34
multiply by 3: 5
divide by 3  : 5
nested       : 1156
consecutive  : 140
while halving: 6

Check: ⌈100/3⌉=34\lceil 100/3 \rceil = 34; 34×34=115634 \times 34 = 1156; 30,31,…,34=81≤1003^0, 3^1, \dots, 3^4 = 81 \le 100 gives 5 values; 40+100=14040 + 100 = 140; 100→50→25→12→6→3→1100 \to 50 \to 25 \to 12 \to 6 \to 3 \to 1 is 6 halvings.

Key takeaways

  • Simple statements cost O(1)O(1); a loop costs iterations × body cost.
  • Adding a constant cc each time: ⌈n/c⌉=Θ(n)\lceil n/c \rceil = \Theta(n) iterations; multiplying or dividing by c≥2c \ge 2: Θ(log⁡n)\Theta(\log n).
  • Nested loops multiply; consecutive loops add (largest term wins).
  • if–else costs the test plus the larger branch.

Practice questions

Answer on paper first, then check. The explanation matters more than the letter you chose.

Question 1 · multiple choice

How many times does the body of for (i = 1; i <= 50; i += 4) run?

Show answer

Answer: B. 13

i = 1, 5, 9, …, 49: that is ⌈50/4⌉ = 13 values. 12 forgets the first value i = 1.

Question 2 · multiple choice

What is the running time of for (i = 1; i <= n; i *= 2) with an O(1)O(1) body?

Show answer

Answer: C. Θ(log⁡n)\Theta(\log n)

i takes the values 1, 2, 4, …, up to n: about log₂n + 1 values. n/2 would be the count for i += 2.

Question 3 · multiple choice

What does this program print?

C · quiz_loops.c
#include <stdio.h>

int main(void)
{
    int count = 0;
    for (int i = 1; i <= 16; i *= 2)
        for (int j = 1; j <= 10; j++)
            count++;
    printf("%d\n", count);
    return 0;
}
Show answer

Answer: C. 50

The outer loop runs for i = 1, 2, 4, 8, 16 (5 times) and the inner loop 10 times each: 5 × 10 = 50. 40 forgets i = 16; 160 treats the outer loop as 16 iterations.

Question 4 · multiple choice

Two consecutive loops run nn and n2n^2 times with O(1)O(1) bodies. The total is:

Show answer

Answer: B. Θ(n2)\Theta(n^2)

Consecutive costs add: n + n² = Θ(n²). Multiplying (n³) is for nested loops.

Question 5 · multiple choice

if (x > 0) { O(n) loop } else { O(n^2) loop } has worst-case cost:

Show answer

Answer: D. O(n2)O(n^2)

The worst case takes the larger branch, O(n²). The two branches never both run, so their costs are not multiplied or added.

Question 6 · short answer

Find the exact number of times x++ runs in for (i = 1; i <= n; i++) for (j = 1; j <= i; j++) x++; and give its order.

Show worked answer

For each i the inner loop runs i times, so the total is 1 + 2 + … + n = n(n + 1)/2. For n = 10 that is 55. Since n(n + 1)/2 = n²/2 + n/2, the running time is Θ(n²).

↑ ↓ move↵ openTab switch type