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) and f(n) with non-negative values:
Notation
Meaning
Condition (constants C>0, n0; all n≥n0)
T(n)=O(f(n))
T grows at most like f
T(n)≤Cf(n)
T(n)=Ω(f(n))
T grows at least like f
T(n)≥Cf(n)
T(n)=Θ(f(n))
T grows like f
both of the above
For example, 1000n is larger than n3 while n<1000≈31.6, but from n=32 on n3 is larger; so 1000n=O(n3), while n3=O(n).
The rules
Construct
Running time
Example
Sequence of simple statements (no loops, calls or recursion)
O(1)
x = x + 5; x = x - 5;
Loop running k times with an O(1) body
O(k)
—
for (i = 1; i <= n; i += c), constant c≥1
⌈n/c⌉ iterations: Θ(n)
step 3, n=100: 34 iterations
for (i = 1; i <= n; i *= c) or for (i = n; i > 0; i /= c), constant c≥2
about logcn iterations: Θ(logn)
c=3, n=100: 5 iterations
Nested loops
product of the iteration counts
two loops of n: Θ(n2)
Consecutive loops
sum of the costs
m then n iterations: O(m+n)
if (cond) S1 else S2
cost of the test + the larger of S1, S2 (worst case)
—
while / repeat loop
(number of iterations) × (cost of one iteration); find the count from how the loop variable changes
while (k > 1) k = k / 2; runs ⌊log2k⌋ times
The multiplying loop needs c≥2: with c=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 : 34multiply by 3: 5divide by 3 : 5nested : 1156consecutive : 140while halving: 6
Simple statements cost O(1); a loop costs iterations × body cost.
Adding a constant c each time: ⌈n/c⌉=Θ(n) iterations; multiplying or dividing by c≥2: Θ(logn).
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 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²).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/complexity-analysis-of-simple-algorithms/analysing-simple-constructs/ to read this page.