An interactive guide to mastering computational complexity — from O(1) to O(n!) — built for your ISC exam.
The measure of how much data the algorithm processes. Could be array length, number of nodes in a tree, digits in a number, or rows in a matrix. As n grows, runtime changes.
The number of elementary operations (comparisons, assignments, arithmetic) an algorithm performs. We express this as a function of n and classify it using Big O.
An O(n²) algorithm on 1 million elements takes ~10¹² operations. An O(n log n) sort takes ~20 million. Choosing the right algorithm is often more important than faster hardware.
| Notation | Name | Rating | n=10 | n=100 | n=1000 | Example |
|---|---|---|---|---|---|---|
| O(1) | Constant | Excellent | 1 | 1 | 1 | Array access by index |
| O(log n) | Logarithmic | Great | 3 | 7 | 10 | Binary Search |
| O(n) | Linear | Good | 10 | 100 | 1,000 | Linear Search |
| O(n log n) | Linearithmic | Fair | 33 | 664 | 9,966 | Merge Sort, Quick Sort (avg) |
| O(n²) | Quadratic | Poor | 100 | 10,000 | 1,000,000 | Bubble Sort, Selection Sort |
| O(2ⁿ) | Exponential | Terrible | 1,024 | 10³⁰ | 10³⁰¹ | Recursive Fibonacci (naive) |
| O(n!) | Factorial | Unusable | 3.6M | ∞ | ∞ | Brute-force permutations |
Drag n to see which term dominates the total count
Big O rule: Drop constants and lower-order terms.
3n³ + 5n² + 100n + 500 → O(n³)
because for large n, the n³ term is overwhelmingly dominant.
The minimum number of operations needed. Occurs when the input is perfectly arranged for the algorithm. Rare in practice.
Expected performance over all possible inputs. Usually requires probability analysis. Most realistic measure for real-world use.
The maximum operations needed. Guaranteed upper bound — algorithm never performs worse than this. Most commonly used in analysis.
| Case | Scenario | Operations | Big O / Notation |
|---|---|---|---|
| Best | Target is at index 0 (first element) | 1 comparison | Ω(1) |
| Average | Target found somewhere in middle | n/2 comparisons | Θ(n) |
| Worst | Target at last position or not found | n comparisons | O(n) |
ISC Exam tip: When asked for the complexity of an algorithm, always state the worst case using Big O unless specifically asked for best or average case. For Linear Search, the answer is O(n).
Target not found — must check all n elements. The loop runs n times.
Best: first element. Average: middle. Worst: last or not present.
No auxiliary data structure needed. Only a loop variable.
Does not require the array to be sorted. Simple but inefficient for large datasets.
Each iteration halves the search space. For n=1024, only 10 comparisons needed.
Best: target is the midpoint on first try. Worst: keep halving until 1 element.
Binary search only works on sorted data. Sorting first = O(n log n) + O(log n) = O(n log n) overall.
After k steps, n/2ᵏ = 1, so k = log₂(n). The search space halves each time.
Two nested loops: outer runs n−1 times, inner runs n−i−1 times. Total ≈ n(n−1)/2 comparisons.
Best (already sorted, with flag optimization): O(n). Without flag: always O(n²).
n(n−1)/2 is a quadratic expression. Drop constants → O(n²).
Equal elements maintain their original relative order after sorting.
Always performs n(n−1)/2 comparisons regardless of input. No early exit possible.
Unlike Bubble Sort, Selection Sort is always O(n²) — even on sorted arrays.
At most n−1 swaps (one per pass) vs Bubble Sort's O(n²) swaps. Better when swaps are expensive.
Long-range swaps can disrupt the relative order of equal elements.
Worst case: reverse-sorted array. Each element must shift all the way to the beginning.
Best case (already sorted): inner while never executes → O(n). Excellent for nearly-sorted data!
Used in hybrid algorithms (like TimSort) for small subarrays. Low overhead, stable, in-place.
Always O(n log n) — even for worst-case input. Derived from recurrence T(n) = 2T(n/2) + n.
All three cases are the same — guaranteed O(n log n). More predictable than Quick Sort.
Split into 2 halves [2T(n/2)] + merge step [O(n)]. Master theorem → O(n log n).
Unlike Bubble/Selection/Insertion, Merge Sort needs extra space for the merge step.
| Algorithm | Best | Average | Worst | Space | Stable? |
|---|---|---|---|---|---|
| Linear Search | Ω(1) | Θ(n) | O(n) | O(1) | — |
| Binary Search | Ω(1) | Θ(log n) | O(log n) | O(1) | — |
| Bubble Sort | Ω(n) | Θ(n²) | O(n²) | O(1) | Yes |
| Selection Sort | Ω(n²) | Θ(n²) | O(n²) | O(1) | No |
| Insertion Sort | Ω(n) | Θ(n²) | O(n²) | O(1) | Yes |
| Merge Sort | Ω(n log n) | Θ(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | Ω(n log n) | Θ(n log n) | O(n²) | O(log n) | No |