Skip to content
Azhimathi Mayam logoDSA Study HubPCCST303 · KTU · S3
Module 1 · syllabusAsked 5× in KTU papersLesson live

Performance analysis: time and space complexity

Measure an algorithm without a stopwatch: space S(P) = c + Sp, step counts, and the loop tricks KTU asks every single time.

Syllabus says: “Performance Analysis - Time & Space Complexity”

On this page · 11 sections
ormakkurippu ✎
Never-forget card

Loop that ADDS (i++) → n. Loop that MULTIPLIES or DIVIDES (j*=2, j/=2) → log n. Nested → MULTIPLY them.

Picture it

Climbing stairs one step at a time takes n steps. Taking the KSRTC lift that doubles the floor each stop gets you there in log n stops.

Write this in the exam

Time complexity is the number of steps an algorithm executes as a function of the input size n; space complexity S(P) = c + Sp(I), a fixed part plus an instance-dependent part.

കൂട്ടിയാൽ n, ഇരട്ടിച്ചാൽ log n — അത്രേ ഉള്ളൂ!

What you will be able to do

  • Compute space complexity using S(P) = c + Sp(I).
  • Build a step-count (frequency count) table.
  • Find the time complexity of nested and logarithmic loops (asked in all 3 KTU papers).
  • Explain best, worst and average case, and present complexity with tables and graphs.

The story

Two friends write programs for the same task. On a gaming laptop one finishes in 1 second; on a college lab PC the other takes 3. Whose algorithm is better? Seconds can't tell you — they depend on the machine. So we count how many basic steps the algorithm does as the input grows. That count doesn't depend on the hardware.

Definitions

Definition

Space complexity: the amount of memory a program needs to run to completion. Time complexity: the amount of computer time it needs, measured as a count of steps.

S(P) = c + Sp(I)

  • c — fixed part: instruction space, simple variables, constants. Does not depend on the input.
  • Sp(I) — variable part: depends on the instance I, e.g. an array of n elements, or the recursion stack (depth × space per call).

Example: an iterative sum of n numbers needs Sp = 0 extra space if the array is passed by address. A recursive sum needs one stack frame per call (n + 1 calls), so Sp(n) grows linearly with n.

Step count (frequency count) method

Write the steps per execution (s/e) of each line and how many times it runs (frequency). Multiply and add.

Total = 2n + 3 steps → O(n). The loop test runs n + 1 times (the last test fails and exits).
Statements/eFrequencyTotal steps
float sum(float a[], int n) {0–0
float s = 0;111
for (i = 0; i < n; i++)1n + 1n + 1
s += a[i];1nn
return s;111
}0–0

The loop rules (memorise these)

LoopIterationsOrder
for(i=0; i<n; i++)nO(n)
for(i=1; i<=n; i=i*2)⌊log₂n⌋ + 1O(log n)
for(j=n; j>0; j=j/2)⌊log₂n⌋ + 1O(log n)
two nested loops 0..nn × nO(n²)
for(i=0;i<n;i++) for(j=0;j<i;j++)0+1+…+(n−1) = n(n−1)/2O(n²)
n loop × halving loopn(⌊log₂n⌋ + 1)O(n log n)
Exam tipKTU Nov 2025 Q1 / May 2026 Q1

The outer loop runs n times. In the inner loop j goes n, n/2, n/4, … , 1 (or 1, 2, 4, … ≤ n), which is ⌊log₂n⌋ + 1 iterations. The print runs n(⌊log₂n⌋ + 1) times, so T(n) = O(n log n).

Exam tipKTU model paper Q1 — the trap

j = 1 is set once, outside both loops. The while loop only runs when j has fallen behind i. Across the entire run j doubles about log₂n times in total. So the work is (n − 1) outer iterations + about log₂n doublings = O(n), not O(n log n).

Tables and graphs (model paper Q9a)

Step counts for common complexities. Double n and see what happens to each column.
nlog₂nnn log₂nn²2ⁿ
101012
212244
42481616
8382464256
164166425665,536
325321601,0244,294,967,296
Growth of common functions
log nnn log nn²2ⁿntime
Plotting time against n shows the same story: 2ⁿ explodes, n² grows fast, n log n and n stay manageable, log n is almost flat.

Best, worst and average case

  • Best case — the minimum steps over all inputs of size n (linear search: key at index 0 → 1 comparison).
  • Worst case — the maximum steps (key absent → n comparisons). Exams usually want this.
  • Average case — the expected steps over all inputs (key equally likely anywhere → about (n + 1)/2).

Common mistakes

  • Writing O(n²) for every nested loop — check how the inner variable changes.
  • Missing the n + 1 frequency of a loop test.
  • Resetting j in your head when the code does not (the model-paper trap).
  • Giving time in seconds instead of as a function of n.

Asked in KTU exams (5)

Quick check — can you get them all?

Question 1

Q1.for(i=0;i<n;i++) for(j=n;j>0;j=j/2) printf("x"); Complexity?

Question 2

Q2.j = 1; for(i=1;i<n;i++) while(j<i) j=j*2; Complexity?

Question 3

Q3.In S(P) = c + Sp(I), Sp(I) is…

Practice

Every visualizer has a Practice mode: predict the next state before the animation reveals it. You can also work through the topic's question set on the practice page.

Exam-ready summary

  • S(P) = c + Sp(I): fixed part + instance part.
  • Step count table: s/e × frequency, then add.
  • +1 loops → n; ×2 or ÷2 loops → log n; nested → multiply.
  • Best / worst / average case; worst case is the usual answer.

References

  • Horowitz, Sahni & Anderson-Freed, Fundamentals of Data Structures in C, 2/e — §1.5 (Performance analysis)
  • Cormen, Leiserson, Rivest & Stein, Introduction to Algorithms, 3/e — §2.2

Explanations on this page are original. Textbook references point to where the standard treatment can be read; no text is reproduced from them.

Prefer revising from notes? Module-wise notes and question papers on Azhimathi Mayam (opens in a new tab)