Analysis of Algorithm

When designing algorithms, the big question is: “How fast will this run as my data grows?” That’s where asymptotic analysis steps in.

What Is Asymptotic Analysis?

  • Focuses on input size (n) as it grows towards infinity
  • Ignores machine-specific constants and lower-order terms
  • Gives an abstract, yet realistic idea of algorithm performance

The Three Big Notations

NotationMeaningDescribes
Big-O (O)Upper bound – Worst caseMax time/resources used
Omega (Ω)Lower bound – Best caseMinimum guarantee
Theta (Θ)Tight bound – Average caseTypical performance

Think of it like this: Big-O is the “maximum damage”, Ω is “best hope”, and Θ is your “expected reality”.


Common Time Complexities

ComplexityDescriptionExample Algorithms
O(1)ConstantAccessing array element
O(log n)LogarithmicBinary Search
O(n)LinearTraversing a list
O(n log n)LinearithmicMerge Sort, Heap Sort
O(n²)QuadraticBubble Sort, Insertion Sort
O(2ⁿ), O(n!)Exponential/FactorialRecursive backtracking (TSP)

🔍 Examples: Finding Complexity

  • Loop Example:

        for i in range(n):
            print(i)
    

    Runs n times → O(n)

  • Nested Loop:

        for i in range(n):
            for j in range(n):
                print(i, j)
    

    Runs n * n times → O(n²)

  • Divide and Conquer (e.g. Merge Sort):Splits into halves each time → O(n log n)