ArticleslgStudy

computer science

Worst-case complexity

Worst-case complexity is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Worst-case complexity rather than just read about it. In short: In computer science (specifically computational complexity theory), the worst-case complexity measures the resources (e.g. running time, memory) that an algorithm requires given an input of arbitrary size (commonly denoted as n in asymptotic notation). It gives an upper bound on the resources required by the algorithm.

Key takeaways

  • Worst-case complexity belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Worst-case complexity to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Worst-case complexity from memory before moving on to harder problems.

Reference excerpt

In computer science (specifically computational complexity theory), the worst-case complexity measures the resources (e.g. running time, memory) that an algorithm requires given an input of arbitrary size (commonly denoted as n in asymptotic notation). It gives an upper bound on the resources required by the algorithm. In the case of running time, the worst-case time complexity indicates the longest running time performed by an algorithm given any input of size n, and thus guarantees that the algorithm will finish in the indicated period of time. The order of growth (e.g. linear, logarithmic) of the worst-case complexity is commonly used to compare the efficiency of two algorithms. The worst-case complexity of an algorithm should be contrasted with its average-case complexity, which is an average measure of the amount of resources the algorithm uses on a random input.

Definition Given a model of computation and an algorithm 𝖠 that halts on each input s, the mapping t𝖠:{0,1}⋆→ℕ is called the time complexity of 𝖠 if, for every input string s, 𝖠 halts after exactly t𝖠(s) steps. Since we usually are interested in the dependence of the time complexity on different input lengths, abusing terminology, the time complexity is sometimes referred to the mapping t𝖠:ℕ→ℕ, defined by the maximal complexity

t𝖠(n):=maxs∈{0,1}nt𝖠(s) of inputs s with length or size ≤n. Similar definitions can be given for space complexity, randomness complexity, etc.

Ways of speaking Very frequently, the complexity t𝖠 of an algorithm 𝖠 is given in asymptotic Big-O Notation, which gives its growth rate in the form t𝖠=O(g(n)) with a certain real valued comparison function g(n) and the meaning:

There exists a positive real number M and a natural number n0 such that |t𝖠(n)|≤Mg(n) for all n≥n0. Quite frequently, the wording is:

„Algorithm 𝖠 has the worst-case complexity O(g(n)).“ or even only:

„Algorithm 𝖠 has complexity O(g(n)).“

Examples Consider performing insertion sort on n numbers on a random-access machine. The best-case for the algorithm is when the numbers are already sorted, which takes O(n) steps to perform the task. However, the input in the worst-case for the algorithm is when the numbers are reverse sorted and it takes O(n2) steps to sort them; therefore the worst-case time-complexity of insertion sort is of O(n2).

See also Analysis of algorithms

References Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGraw-Hill, 2001. ISBN 0-262-03293-7. Chapter 2.2: Analyzing algorithms, pp.25-27. Oded Goldreich. Computational Complexity: A Conceptual Perspective. Cambridge University Press, 2008. ISBN 0-521-88473-X, p.32.

Worked examples

Example 1 — a first encounter with Worst-case complexity

Start with the simplest possible case. Write down what Worst-case complexity claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Worst-case complexity before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Worst-case complexity ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Worst-case complexity

In research
Worst-case complexity appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Worst-case complexity in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Worst-case complexity is common in secondary-school and first-year university syllabi. It links to neighbouring topics Analysis of algorithms, so understanding it makes those chapters shorter.
In everyday life
Look for Worst-case complexity outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Worst-case complexity” →

How to study Worst-case complexity in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Worst-case complexity means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Worst-case complexity out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Worst-case complexity in simple terms?

In computer science (specifically computational complexity theory), the worst-case complexity measures the resources (e.g. running time, memory) that an algorithm requires given an input of arbitrary size (commonly denoted as n in asymptotic notation). It gives an upper bound on the resources requi…

Why does Worst-case complexity matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Worst-case complexity?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Worst-case complexity.

Tags

  • Analysis of algorithms

Keep exploring