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: grows in proportion to the input, grows with its square.
Move the input size below and watch the growth curves separate.
Slide n and watch which growth curves actually matter
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 :
- if there exist constants and such that for all . This is an upper bound.
- if beyond some point — a lower bound.
- if both hold — a tight bound.
The constants and 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: is .
- Keep the dominant term: is .
- Sequential code adds, and the sum keeps the larger: a loop then another loop is .
- Nested code multiplies: an inner loop of inside an outer loop of is .
- Divide and conquer gives recurrences such as , whose solution is .
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: on average but on adversarial pivots.
Big-O hides constants that can dominate
An algorithm with a heavy constant can lose to an algorithm for every input you will realistically see. Asymptotics describe large- behaviour; small- 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 auxiliary space; merge sort is . Amortised analysis spreads a rare expensive operation over the cheap ones around it — the reason appending to a dynamic array is called despite the occasional reallocation.
Illustrative vs real
The chart computes exact operation counts for a handful of growth classes at small 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
From the assignment paper
Modeled on NITJ AI-507, Assignment/Quiz
0 / 5 answered
Where next: sorting — arranging data in order, and the comparison-based algorithms that do it.