Home
DSA Course
Basics
Time Complexity
Time Complexity
Time complexity isn't about measuring how many seconds an algorithm takes. It's about measuring how the number of operations grows as the input size (N) grows.
Input Size (N): 10
Intuition
Time complexity isn't about measuring how many seconds an algorithm takes. It's about measuring how the number of operations grows as the input size (N) grows.
Concept
- O(1) Constant: Time doesn't change with N (e.g., accessing array index).
- O(log n) Logarithmic: Time grows slowly (e.g., Binary Search).
- O(n) Linear: Time grows directly with N (e.g., simple loop).
- O(n log n): Common in efficient sorting (e.g., Merge Sort).
- O(n²) Quadratic: Time grows rapidly (e.g., nested loops).
How it Works
Analyzing Code:
- Count the loops.
- Check if loops are nested.
- See if the loop range depends on N.
- Ignore constants (O(2n) becomes O(n)).
Step-by-Step Breakdown
Use the interactive chart:
- Slider: Increase N to see how the curves diverge.
- Toggle: Click buttons to isolate specific complexities.
- Observe: Notice how O(n²) skyrockets compared to O(n).
When to Use
- Always! Understanding complexity is key to writing efficient code.
- When optimizing for large datasets.
When NOT to Use
- For very small N, readability might be more important than optimization.
How to Identify
"Optimize performance", "Constraints: N ≤ 10^5", "Time Limit Exceeded".
Sample Problems
Frequently Asked Questions
What is Time Complexity?
Time complexity isn't about measuring how many seconds an algorithm takes. It's about measuring how the number of operations grows as the input size (N) grows.
What is the time complexity of Time Complexity?
The time complexity is: Best case Varies, Average case Varies, Worst case Varies. Space complexity is varies.
