What is Time Complexity?

Time Complexity tells us how the running time of an algorithm grows when the input size n increases.

It is commonly represented using Big O Notation.

Asymptotic Notations

NotationMeaning
Ω (Omega)Lower Bound / Best Case
Θ (Theta)Tight Bound / Average Case
O (Big O)Upper Bound / Worst Case

Linear Search

CaseComplexity
BestO(1)
AverageO(n)
WorstO(n)

Common Time Complexities

ComplexityNameExample
O(1)ConstantArray access
O(log n)LogarithmicBinary Search
O(n)LinearArray traversal
O(n log n)LinearithmicMerge Sort, Heap Sort
O(n²)QuadraticNested loops
O(2ⁿ)ExponentialRecursive brute force
O(n!)FactorialPermutations

1. O(1) – Constant Time

Direct access takes O(1).

2. O(n) – Linear Time

Loop runs n times → O(n).

3. O(log n) – Logarithmic

Whiteboard
Whiteboard diagram

Why O(log n)?

At each comparison, half of the tree is eliminated.

Example: Binary SearchTime Complexity: O(log n)

The search space is repeatedly divided in half.

4. O(n log n) – Linearithmic

Whiteboard
Whiteboard diagram
  • Divide: The array is repeatedly split in half, creating log₂(n) levels.
  • Merge: At each level, all n elements are merged exactly once.
  • Total: O(n) × O(log n) = O(n log n). This is why Merge Sort has O(n log n) time complexity in the best, average, and worst cases.

  • Examples:
    Merge Sort
    Heap Sort
    Quick Sort (Average)
    Time Complexity: O(n log n)

    5. O(n²) – Quadratic

    n × n = n² Time Complexity: O(n²) 

    For n = 3 → 3 x 3 = 9 operations.

    Rules for Calculating TC

    RuleExampleResult
    Ignore constantsO(5n)O(n)
    Ignore lower termsO(n + 100)O(n)
    Sequential loopsO(n) + O(n)O(n)
    Different sequential loopsO(n) + O(m)O(n+m)
    Nested loopsO(n) × O(n)O(n²)
    Different nested loopsO(n) × O(m)O(nm)

    Sequential Loops

    Nested Loops

     O(n) × O(n) = O(n²)

    Complexity Order

     O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ) < O(n!)

    Lower complexity = generally better performance for large inputs.