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
Loop that ADDS (i++) → n. Loop that MULTIPLIES or DIVIDES (j*=2, j/=2) → log n. Nested → MULTIPLY them.
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.
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
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.
| Statement | s/e | Frequency | Total steps |
|---|---|---|---|
| float sum(float a[], int n) { | 0 | – | 0 |
| float s = 0; | 1 | 1 | 1 |
| for (i = 0; i < n; i++) | 1 | n + 1 | n + 1 |
| s += a[i]; | 1 | n | n |
| return s; | 1 | 1 | 1 |
| } | 0 | – | 0 |
The loop rules (memorise these)
| Loop | Iterations | Order |
|---|---|---|
| for(i=0; i<n; i++) | n | O(n) |
| for(i=1; i<=n; i=i*2) | ⌊log₂n⌋ + 1 | O(log n) |
| for(j=n; j>0; j=j/2) | ⌊log₂n⌋ + 1 | O(log n) |
| two nested loops 0..n | n × n | O(n²) |
| for(i=0;i<n;i++) for(j=0;j<i;j++) | 0+1+…+(n−1) = n(n−1)/2 | O(n²) |
| n loop × halving loop | n(⌊log₂n⌋ + 1) | O(n log n) |
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).
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)
| n | log₂n | n | n log₂n | n² | 2ⁿ |
|---|---|---|---|---|---|
| 1 | 0 | 1 | 0 | 1 | 2 |
| 2 | 1 | 2 | 2 | 4 | 4 |
| 4 | 2 | 4 | 8 | 16 | 16 |
| 8 | 3 | 8 | 24 | 64 | 256 |
| 16 | 4 | 16 | 64 | 256 | 65,536 |
| 32 | 5 | 32 | 160 | 1,024 | 4,294,967,296 |
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)
- November 2025 · Q13 markssolved ✓Compute the time complexity of the following code fragment. for(i=0;i<n;i++) for(j=n;j>0;j=j/2) printf("DATA STRUCTURE");
- May 2026 · Q13 markssolved ✓Compute the time complexity of the following code fragment. for(i=1; i<=n; i++) for(j=1; j<=n; j=j*2) printf("KTU");
- May 2026 · Q10b3 markssolved ✓Find the Best, Worst and average case time complexity of Insertion sort.
- — 2024 · Q13 marksWhat is the time complexity of this algorithm? j = 1 for (i = 1; i < n; i++) while (j < i) j = j * 2
- — 2024 · Q9a5 marksIllustrate how an algorithm's time and space complexity can be explained using tables and graphs.
Quick check — can you get them all?
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)