1.6Analysis of Algorithms: Best, Worst and Average Case
Covers IGNOU MCS-211 §1.5
Knowing that one algorithm is “faster” is not enough; we need to say how much faster and on which inputs. This page defines time and space complexity, shows how the same algorithm can behave differently on different inputs of the same size, and introduces the notations used to describe growth.
Time and space complexity
The study of how much time and memory algorithms use is called analysis of algorithms; the term was introduced by Donald Knuth.
A key operation (basic operation) is the operation that dominates the running time, chosen so that every other operation is done at most a constant number of times per key operation. For searching and sorting, the key operation is the comparison of two elements. Unless stated otherwise, “complexity” means time complexity.
Three cases
For many algorithms, inputs of the same size need different numbers of steps. Searching for a key that is at the front of an array is quicker than searching for one at the back.
Case
Meaning
Why we study it
Worst case Cworst(n)
Maximum number of key operations over all inputs of size n
A guarantee: the algorithm never does worse. Used by default.
Best case Cbest(n)
Minimum number over all inputs of size n
Shows the most favourable inputs; rarely a guarantee of anything.
Average case Cavg(n)
Expected value of the number of key operations, under a stated probability distribution of inputs
Describes typical behaviour; harder to compute and depends on assumptions.
The expected value: if the algorithm performs N1,N2,…,Nk operations with probabilities p1,p2,…,pk (where p1+⋯+pk=1), then
E=N1p1+N2p2+⋯+Nkpk.
Linear search
Linear (sequential) search looks for a value x in an array A[1..n] by comparing x with A[1],A[2],… until it finds x or reaches the end. It returns the location loc, or 0 if x is not present.
Linear search pseudocode
Algorithm LinearSearch(A[1..n], x)loc ← 0k ← 1while loc = 0 and k ≤ n do if x = A[k] then loc ← k end if k ← k + 1end whilereturn loc // 0 means "x is not in A"
C · search_bug.c
#include <stdio.h>/* The loop as printed: k = k + 1 sits INSIDE the if-block. A safety limit stops the demonstration after 10 rounds. */int main(void){ int A[] = {0, 42, 17, 8}; /* A[1..3] */ int n = 3, x = 8; int loc = 0, k = 1, rounds = 0; while (loc == 0 && k <= n) { if (x == A[k]) { loc = k; k = k + 1; } rounds++; if (rounds == 10) { printf("still at k = %d after %d rounds: loop never ends\n", k, rounds); return 0; } } printf("found at %d\n", loc); return 0;}
Output
still at k = 1 after 10 rounds: loop never ends
The corrected version, which also counts comparisons:
C · linear_search.c
#include <stdio.h>/* Linear search on A[1..n] (position 0 is not used, to match the 1-based pseudocode). Returns loc, or 0 if x is not present. *comparisons receives the number of comparisons x == A[k]. */int linear_search(const int A[], int n, int x, int *comparisons){ int loc = 0; int k = 1; *comparisons = 0; while (loc == 0 && k <= n) { (*comparisons)++; if (x == A[k]) loc = k; k = k + 1; /* outside the if: always move to the next element */ } return loc;}int main(void){ int A[] = {0, 42, 17, 8, 99, 23}; /* A[1..5] */ int n = 5; int tests[] = {42, 23, 50}; int c; for (int t = 0; t < 3; t++) { int loc = linear_search(A, n, tests[t], &c); if (loc == 0) printf("x = %2d: not present, comparisons = %d\n", tests[t], c); else printf("x = %2d: found at location %d, comparisons = %d\n", tests[t], loc, c); } /* Average over all successful searches, each position equally likely */ int total = 0; for (int i = 1; i <= n; i++) { linear_search(A, n, A[i], &c); total += c; } printf("average comparisons (successful) = %d/%d = %.1f, (n+1)/2 = %.1f\n", total, n, (double)total / n, (n + 1) / 2.0); return 0;}
Output
x = 42: found at location 1, comparisons = 1x = 23: found at location 5, comparisons = 5x = 50: not present, comparisons = 5average comparisons (successful) = 15/5 = 3.0, (n+1)/2 = 3.0
Analysing linear search
Let C(n) be the number of comparisons between x and array elements.
Best case:x=A[1]. One comparison: Cbest(n)=1.
Worst case:x is the last element or not present. All n elements are compared: Cworst(n)=n.
Average case (assume x is present and equally likely to be at any of the n positions, each with probability n1):
If the search succeeds only with probability p (Levitin, §2.1), the same method gives Cavg(n)=2p(n+1)+n(1−p). For p=1 this is (n+1)/2; for p=0 it is n.
A first look at O, Ω and Θ
To describe growth without constant factors we use three asymptotic notations (they describe behaviour as n→∞). Unit 2 studies them in detail; here are the definitions you need now.
Notation
Meaning
Formal condition (for some constants c,c1,c2>0 and n0, for all n≥n0)
f(n)=O(g(n))
Upper bound: f grows at most like g
0≤f(n)≤cg(n)
f(n)=Ω(g(n))
Lower bound: f grows at least like g
0≤cg(n)≤f(n)
f(n)=Θ(g(n))
Tight bound: f grows exactly like g
c1g(n)≤f(n)≤c2g(n)
For example, 3n+2=Θ(n): take c1=3, c2=4, n0=2, since 3n≤3n+2≤4n for all n≥2.
A bound and a case are different things. The case (best, worst, average) chooses which function we study; the notation (O, Ω, Θ) describes how that function grows. For linear search, Cworst(n)=n=Θ(n), Cbest(n)=1=Θ(1) and Cavg(n)=(n+1)/2=Θ(n). Each case has its own tight bound.
Key takeaways
Time complexity counts key operations; space complexity counts extra memory, both as functions of n.
Worst case = maximum, best case = minimum, average case = expected value over a stated input distribution.
Linear search: best 1, worst n, average (n+1)/2 comparisons (successful search).
O = upper bound, Ω = lower bound, Θ = tight bound; these are not the same as worst, best, average.
Practice questions
Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 6 · short answer
Show that 5n+3=Θ(n) by finding constants.
Show worked answer
Lower bound: 5n ≤ 5n + 3 for all n ≥ 1, so c₁ = 5. Upper bound: 5n + 3 ≤ 6n when 3 ≤ n, so c₂ = 6 with n₀ = 3. Hence 5n ≤ 5n + 3 ≤ 6n for all n ≥ 3, and 5n + 3 = Θ(n).
MCA Online notes are made for reading on screen. Open https://mcastudy.online/mcs-211/basics-of-an-algorithm-and-its-properties/analysis-of-algorithms-best-worst-and-average-case/ to read this page.