Wiki
Core12 min read

Complexity and asymptotic analysis

Big-O compares how running time grows with input size, ignoring constants and machine speed.

Two programs can both be correct and still differ by a factor of a million once the input gets large. Asymptotic analysis is how we reason about that difference before running anything: it describes how the number of operations grows as the input size grows, stripped of machine speed and constant factors.

Compare shapes, not stopwatch readings

A faster computer multiplies every running time by the same constant, so it cannot change which algorithm wins at scale. What matters is the shape of the growth curve. Big-O names that shape: O(n)O(n) grows in proportion to the input, O(n2)O(n^2) grows with its square.

Move the input size below and watch the growth curves separate.

Slide n and watch which growth curves actually matter

O(1)
1
O(log n)
5
O(n)
32
O(n log n)
160
O(n^2)
1.0K
O(2^n)
4.3B

Doubling n barely moves O(log n) and doubles O(n); it multiplies O(n^2) by four and O(2^n) by the entire previous count. Asymptotic notation throws away constants for exactly this reason: past some point the growth class, not the machine, decides what is feasible.

The formal definitions

For functions f,g:N→R+f, g : \mathbb{N} \to \mathbb{R}^+:

  • f(n)=O(g(n))f(n) = O(g(n)) if there exist constants c>0c > 0 and n0n_0 such that f(n)≤c g(n)f(n) \le c\,g(n) for all n≥n0n \ge n_0. This is an upper bound.
  • f(n)=Ω(g(n))f(n) = \Omega(g(n)) if f(n)≥c g(n)f(n) \ge c\,g(n) beyond some point — a lower bound.
  • f(n)=Θ(g(n))f(n) = \Theta(g(n)) if both hold — a tight bound.

The constants cc and n0n_0 are exactly what we are allowed to ignore. Big-O says "eventually, no worse than a constant multiple", not "fast for small inputs".

Rules of thumb

  • Drop constants: 3n+73n + 7 is O(n)O(n).
  • Keep the dominant term: n2+nlog⁡n+5n^2 + n \log n + 5 is O(n2)O(n^2).
  • Sequential code adds, and the sum keeps the larger: a loop then another loop is O(n)+O(n)=O(n)O(n) + O(n) = O(n).
  • Nested code multiplies: an inner loop of nn inside an outer loop of nn is O(n2)O(n^2).
  • Divide and conquer gives recurrences such as T(n)=2T(n/2)+O(n)T(n) = 2T(n/2) + O(n), whose solution is O(nlog⁡n)O(n \log n).

Which case?

An algorithm can have different bounds depending on the input:

  • Best case — the most favourable input (already sorted).
  • Worst case — the input that forces the most work; the usual headline.
  • Average case — the expected cost over a distribution of inputs, which requires saying what that distribution is.

Quicksort illustrates why the distinction matters: O(nlog⁡n)O(n \log n) on average but O(n2)O(n^2) on adversarial pivots.

Big-O hides constants that can dominate

An O(n)O(n) algorithm with a heavy constant can lose to an O(nlog⁡n)O(n \log n) algorithm for every input you will realistically see. Asymptotics describe large-nn behaviour; small-nn engineering still cares about the hidden constant, the memory layout and the cache.

Space complexity is analysed the same way, counting extra memory rather than operations. In-place sorting is O(1)O(1) auxiliary space; merge sort is O(n)O(n). Amortised analysis spreads a rare expensive operation over the cheap ones around it — the reason appending to a dynamic array is called O(1)O(1) despite the occasional reallocation.

Illustrative vs real

The chart computes exact operation counts for a handful of growth classes at small nn and plots them on a log scale. Real benchmarks include cache misses, branch prediction and I/O, none of which the model captures. Asymptotic analysis is the right tool for choosing between algorithms; profiling is the right tool for tuning an implementation.

Check yourself

Eduspheria wiki · Programming & Data Structures, Algorithms

0 / 5 answered

  1. 1Simplify 4n^2 + 3n + 10 to its tight big-O bound.
    Multiple choice
  2. 2A loop over n items runs an inner loop over n items. What is the total number of inner-body executions for n = 12?
    executions
    Numeric answer
  3. 3Which notation names a tight bound that is both an upper and a lower bound?
    Short answer
  4. 4An O(n) algorithm always runs faster than an O(n log n) algorithm.
    True / false
  5. 5How many times does the body of a single loop over n items run for n = 30?
    times
    Numeric answer

From the assignment paper

Modeled on NITJ AI-507, Assignment/Quiz

0 / 5 answered

  1. 1What does Big-O notation describe?
    Multiple choice
  2. 2As n grows large, which of these complexity classes grows fastest?
    Multiple choice
  3. 3Which notation states a lower bound, matching an algorithm's best case?
    Multiple choice
  4. 4An algorithm runs in O(n log n). What does that imply about its growth?
    Multiple choice
  5. 5A routine performs n log2 n basic operations. For an input of size 8, how many operations is that?
    operations
    Numeric answer

Where next: sorting — arranging data in order, and the comparison-based algorithms that do it.