2.5Efficiency and Asymptotic Analysis
Covers IGNOU MCS-211 §2.5, 2.6, 2.6.1, 2.6.2
Before defining the notations formally, we need to be clear about what we are measuring and why we may ignore some details. This page explains input size, how a running time is counted, why the result does not depend on the computer, and what asymptotic analysis can and cannot tell you.
Input size
The running time is a function of the size of the instance — a number that measures how big the input is. The right measure depends on the problem:
| Problem | Natural size measure |
|---|---|
| Sorting, searching | Number of elements |
| Polynomial evaluation | Degree of the polynomial |
| Matrix multiplication | Dimension of the matrices |
| Graph algorithms | Number of vertices and edges (two parameters) |
| Arithmetic on large numbers, primality | Number of bits of the number |
The last row matters: testing whether is prime by trying all divisors up to takes about steps, which is exponential in the input size , even though it looks “linear in ”.
Counting the running time
The theoretical approach:
- Give each basic instruction (assignment, arithmetic, comparison, read, write) a constant cost.
- Multiply the cost of a statement inside a loop by the number of times the loop runs.
- Add everything to get for a general input of size .
The result does not depend on the programming language or the computer. A faster machine changes every cost by roughly the same constant factor, so if is polynomial (or exponential) on one machine, it is polynomial (or exponential) on every machine.
Asymptotic analysis
Example from the study material: and . For small , is smaller because of its small constant; but the higher power soon dominates.
# Compare T1(n) = 1000 n^2 with T2(n) = 5 n^4 around the crossover point.
for n in range(12, 18):
t1, t2 = 1000 * n * n, 5 * n ** 4
better = "T1" if t1 < t2 else "T2"
print(f"n = {n:2}: T1 = {t1:7}, T2 = {t2:7}, faster: {better}")
first = next(n for n in range(1, 1000) if 1000 * n * n < 5 * n ** 4)
print("T1 is faster for every n >=", first)n = 12: T1 = 144000, T2 = 103680, faster: T2
n = 13: T1 = 169000, T2 = 142805, faster: T2
n = 14: T1 = 196000, T2 = 192080, faster: T2
n = 15: T1 = 225000, T2 = 253125, faster: T1
n = 16: T1 = 256000, T2 = 327680, faster: T1
n = 17: T1 = 289000, T2 = 417605, faster: T1
T1 is faster for every n >= 15Worst-case and average-case analysis
- Worst-case analysis gives an upper limit on the running time over all inputs of size . It is a guarantee and is the default.
- Average-case analysis gives the expected running time over inputs drawn from an assumed distribution. It needs probability and an assumption about inputs that may be hard to justify, but it can describe typical behaviour better. Quicksort is the classic example: worst case proportional to , but expected time proportional to .
Bubble sort shows how one algorithm can have different best and worst cases depending on how it is written:
#include <stdio.h>
#include <stdbool.h>
#define N 10
/* Plain bubble sort: always makes every pass. */
long bubble_plain(int a[], int n)
{
long comparisons = 0;
for (int pass = 0; pass < n - 1; pass++)
for (int j = 0; j < n - 1 - pass; j++) {
comparisons++;
if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; }
}
return comparisons;
}
/* Bubble sort with a flag: stops after a pass with no swaps. */
long bubble_flag(int a[], int n)
{
long comparisons = 0;
for (int pass = 0; pass < n - 1; pass++) {
bool swapped = false;
for (int j = 0; j < n - 1 - pass; j++) {
comparisons++;
if (a[j] > a[j + 1]) { int t = a[j]; a[j] = a[j + 1]; a[j + 1] = t; swapped = true; }
}
if (!swapped)
break;
}
return comparisons;
}
void fill(int a[], int n, bool sorted)
{
for (int i = 0; i < n; i++)
a[i] = sorted ? i : n - i;
}
int main(void)
{
int a[N];
fill(a, N, true); printf("sorted input : plain = %ld, with flag = ", bubble_plain(a, N));
fill(a, N, true); printf("%ld\n", bubble_flag(a, N));
fill(a, N, false); printf("reverse input : plain = %ld, with flag = ", bubble_plain(a, N));
fill(a, N, false); printf("%ld\n", bubble_flag(a, N));
return 0;
}sorted input : plain = 45, with flag = 9
reverse input : plain = 45, with flag = 45Plain bubble sort always makes comparisons for . The version with a “swapped” flag stops after one pass on sorted input: comparisons.
Drawbacks of asymptotic analysis
| Limitation | Example | What to do |
|---|---|---|
| Constants are hidden | and are both , yet one is 100 times slower. | Compare constants or measure when classes are equal. |
| Small inputs are ignored | For , beats . | For small, fixed , a “worse” algorithm may be better (e.g. insertion sort inside library sorts for tiny subarrays). |
| One case may mislead | An algorithm with a better worst case may be slower on the inputs you actually get. | Consider the average case for your expected inputs. |
| Real machines differ | Cache, memory and parallelism are not modelled. | Profile real implementations. |
Key takeaways
- The size of an instance is a problem-specific measure (elements, degree, vertices/edges, bits).
- = sum of constant costs times execution counts; machine speed changes only constants.
- Higher growth rates eventually dominate: for all .
- Plain bubble sort is even in the best case; only the early-stopping version is linear on sorted input.
- Asymptotic analysis hides constants and ignores small inputs.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
An algorithm’s step count is . Find and say which term dominates and what the growth class is.
Show worked answer
T(10) = 2(100) + 3(10) + 1 = 200 + 30 + 1 = 231. The term 2n² gives 200 of the 231 steps and grows fastest, so it dominates; dropping the constant 2 and the lower terms gives growth Θ(n²) (quadratic).