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 📋
Complexity | 10 Items | 1,000 Items | Simple Code Example | What It's Like |
---|---|---|---|---|
O(1) | 1 | 1 | arr[0] | Picking first apple from a basket 🍎 |
O(log n) | ~3 | ~10 | Binary search | Finding a word in dictionary by halving 📖 |
O(n) | 10 | 1,000 | for(let i=0; i<n; i++) | Reading every page in a book 📚 |
O(n log n) | ~30 | ~10,000 | Merge sort | Sorting a deck of cards by dividing 🃏 |
O(n²) | 100 | 1,000,000 | Nested for loops | Handshakes between all classmates 👫👫👫 |
O(2ⁿ) | 1,024 | HUGE! | Recursive Fibonacci | Trying 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
- Write time as algebraic expression
- Find order of growth
- Compare growth rates
Next Steps
- Detailed explanation of asymptotic notations (Big-O, Θ, Ω)
- How to analyze more complex algorithm complexities
- Practical examples of algorithm analysis
- Space complexity analysis