Data Structure and Algorithm
Algorithms
Asymptotic Analysis

Asymptotic Analysis in JavaScript

Introduction to Asymptotic Analysis

Asymptotic analysis is the primary method we use to analyze algorithms. It focuses on:

  • Measuring the order of growth of time taken by a program in terms of input size (n)
  • Using notations like Θ (Theta), O (Big-O), and Ω (Omega)
  • Being independent of programming language, machine, or test cases

Core Concepts

  • Input Size (n): Number of elements/operations
  • Growth Rate: How runtime/memory scales with n
  • Machine Independence: Analysis valid across environments

Key Characteristics

Machine/Language Independent

  • Doesn't depend on CPU speed or programming language
  • Focuses purely on algorithmic efficiency

No Implementation Required

  • Can analyze algorithms just from pseudocode
  • Don't need to actually run the code

Worst-Case Focused

  • Typically analyzes worst-case scenarios
  • Provides guaranteed upper bounds

Time Complexity Analysis Examples

Constant Time Function (O(1))

function sumFormula(n) {
    return n * (n + 1) / 2;  // 1 multiplication, 1 addition, 1 division
}

Time Expression: C₁

  • Always takes constant time regardless of input size
  • Arithmetic operations (+, *, /) considered constant time

Linear Time Function (O(n))

function sumLoop(n) {
    let sum = 0;             // C₂
    for (let i = 1; i <= n; i++) {  // C₃ executed n times
        sum += i;             // C₄ executed n times
    }
    return sum;
}

Time Expression: C₂ + C₃n + C₄n = C₅n + C₆

  • Grows linearly with input size
  • Constants don't affect the growth rate

Quadratic Time Function (O(n²))

function sumNestedLoop(n) {
    let sum = 0;             // C₇
    for (let i = 1; i <= n; i++) {      // C₈ executed n times
        for (let j = 1; j <= i; j++) {  // C₉ executed n(n+1)/2 times
            sum += 1;        // C₁₀ executed n(n+1)/2 times
        }
    }
    return sum;
}

Time Expression: C₇ + C₈n + C₉(n²/2 + n/2) + C₁₀(n²/2 + n/2)

  • Simplifies to C₁₁n² + C₁₂n + C₁₃
  • Dominated by the n² term for large n

Memory Considerations

  • Primitive types: Constant space
  • Objects/Arrays: Linear space
  • Recursion: Call stack space

Time Complexity Cheat Sheet 📋

Complexity10 Items1,000 ItemsSimple Code ExampleWhat It's Like
O(1)11arr[0]Picking first apple from a basket 🍎
O(log n)~3~10Binary searchFinding a word in dictionary by halving 📖
O(n)101,000for(let i=0; i<n; i++)Reading every page in a book 📚
O(n log n)~30~10,000Merge sortSorting a deck of cards by dividing 🃏
O(n²)1001,000,000Nested for loopsHandshakes between all classmates 👫👫👫
O(2ⁿ)1,024HUGE!Recursive FibonacciTrying every locker combination 🔒

Key Takeaways:

  • Green Zone (Good) 🟢: O(1), O(log n), O(n)
  • Yellow Zone (Caution) 🟡: O(n log n)
  • Red Zone (Avoid) 🔴: O(n²), O(2ⁿ)

Analysis Process

  1. Write time as algebraic expression
  2. Find order of growth
  3. Compare growth rates

Next Steps

  1. Detailed explanation of asymptotic notations (Big-O, Θ, Ω)
  2. How to analyze more complex algorithm complexities
  3. Practical examples of algorithm analysis
  4. Space complexity analysis