MCS-211 · Block 1 Unit 2

Some Pre-requisites and Asymptotic Bounds

Section 5 of 8

Block 1 · Unit 2Some Pre-requisites and Asymptotic Bounds11 min · Section 5 of 8

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 nn that measures how big the input is. The right measure depends on the problem:

ProblemNatural size measure
Sorting, searchingNumber of elements nn
Polynomial evaluationDegree nn of the polynomial
Matrix multiplicationDimension nn of the n×nn \times n matrices
Graph algorithmsNumber of vertices VV and edges EE (two parameters)
Arithmetic on large numbers, primalityNumber of bits b≈log⁡2Nb \approx \log_2 N of the number NN

The last row matters: testing whether NN is prime by trying all divisors up to NN takes about N=2bN = 2^b steps, which is exponential in the input size bb, even though it looks “linear in NN”.

Counting the running time

The theoretical approach:

  1. Give each basic instruction (assignment, arithmetic, comparison, read, write) a constant cost.
  2. Multiply the cost of a statement inside a loop by the number of times the loop runs.
  3. Add everything to get T(n)T(n) for a general input of size nn.

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 T(n)T(n) is polynomial (or exponential) on one machine, it is polynomial (or exponential) on every machine.

Asymptotic analysis

Example from the study material: T1(n)=1000n2T_1(n) = 1000n^2 and T2(n)=5n4T_2(n) = 5n^4. For small nn, T2T_2 is smaller because of its small constant; but the higher power soon dominates.

Python · crossover.py
# 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)
Output
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 >= 15

Worst-case and average-case analysis

  • Worst-case analysis gives an upper limit on the running time over all inputs of size nn. 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 n2n^2, but expected time proportional to nlog⁡nn \log n.

Bubble sort shows how one algorithm can have different best and worst cases depending on how it is written:

C · bubble_compare.c
#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;
}
Output
sorted input  : plain = 45, with flag = 9
reverse input : plain = 45, with flag = 45

Plain bubble sort always makes n(n−1)/2=45n(n-1)/2 = 45 comparisons for n=10n = 10. The version with a “swapped” flag stops after one pass on sorted input: n−1=9n - 1 = 9 comparisons.

Drawbacks of asymptotic analysis

LimitationExampleWhat to do
Constants are hidden1000n21000n^2 and 10n210n^2 are both Θ(n2)\Theta(n^2), yet one is 100 times slower.Compare constants or measure when classes are equal.
Small inputs are ignoredFor n≤14n \le 14, 5n45n^4 beats 1000n21000n^2.For small, fixed nn, a “worse” algorithm may be better (e.g. insertion sort inside library sorts for tiny subarrays).
One case may misleadAn 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 differCache, 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).
  • T(n)T(n) = sum of constant costs times execution counts; machine speed changes only constants.
  • Higher growth rates eventually dominate: 1000n2<5n41000n^2 < 5n^4 for all n≥15n \ge 15.
  • Plain bubble sort is Θ(n2)\Theta(n^2) 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 1 · multiple choice

For T1(n)=1000n2T_1(n) = 1000n^2 and T2(n)=5n4T_2(n) = 5n^4, from which nn onwards is T1T_1 always smaller?

Show answer

Answer: B. 15

1000n² < 5n⁴ ⇔ n² > 200 ⇔ n > 14.14, so from n = 15. At n = 14, T₂ is still slightly smaller (192080 < 196000).

Question 2 · multiple choice

What is the natural input size for testing whether a number NN is prime?

Show answer

Answer: C. The number of bits, about log⁡2N\log_2 N

Inputs are measured by how much space they take to write down, and N takes about log₂N bits. Using N itself hides that the algorithm may be exponential.

Question 3 · multiple choice

How many comparisons does plain bubble sort make on a sorted array of 8 elements?

Show answer

Answer: D. 28

Plain bubble sort always makes n(n − 1)/2 = 8 × 7 / 2 = 28 comparisons. 7 is the count for the early-stopping version.

Question 4 · multiple choice

Which is a drawback of asymptotic analysis?

Show answer

Answer: A. Two algorithms with the same order of growth may have very different constant factors.

10n² and 1000n² are both Θ(n²). Asymptotic analysis is language-independent and compares any growth rates.

Question 5 · multiple choice

Quicksort’s running time is proportional to n2n^2 in the worst case and nlog⁡nn \log n on average. This shows that:

Show answer

Answer: C. average-case analysis can describe typical behaviour better than worst-case analysis

Quicksort is fast in practice because bad inputs are rare (or avoided with a random pivot). The worst case alone would make it look as bad as bubble sort.

Question 6 · short answer

An algorithm’s step count is T(n)=2n2+3n+1T(n) = 2n^2 + 3n + 1. Find T(10)T(10) 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).

↑ ↓ move↵ openTab switch type