Choose Complexity Class
Input Size: 10
Execution Trace
Pseudocode
Statistics
Operations
0
Complexity
O(1)
Formula
f(n) = 1
Step Progress
0 / 0
What is time complexity
Time complexity describes how runtime grows as input size n grows. Big-O keeps the dominant term and drops constants. One loop is usually O(n). Nested loops are O(n²). Halving each step is O(log n).
Interactive Complexity Visualizer
Choose Complexity Class
Input Size: 10
Execution Trace
Pseudocode
Statistics
Operations
0
Complexity
O(1)
Formula
f(n) = 1
Step Progress
0 / 0
Complexity Growth Comparison
How Complexity Scales With Input Size
| O(1) | 1 |
| O(log n) | 4 |
| O(n) | 10 |
| O(n log n) | 34 |
| O(n²) | 100 |
| O(2ⁿ) | 1,024 |
Notice: Each complexity class creates dramatically different operation counts!
Understanding Loop Patterns
| Pattern | Code Example | Complexity | Reason |
|---|---|---|---|
| Halving Loop | n = n / 2 each iteration |
O(log n) | Range halves each step (log₂ steps needed) |
| Single Loop | for i in 0..n |
O(n) | Loop runs n times |
| Loop + Halving | for each, halve remaining |
O(n log n) | n iterations × log n work each = n log n |
| Nested Loops | for i in 0..n: for j in 0..n |
O(n²) | n × n iterations = n² total |
Key Concepts
Halving Pattern
n → n/2 → n/4 → ... = log n steps
Single Loop
Loop runs n times = O(n)
Nested Loops
Loop inside loop = n × n = O(n²)
Loop + Halving
n iterations × log n work = O(n log n)
Scalability
O(n) processes 1M items. O(n²) takes 1M² times longer!
Choose Wisely
Algorithm choice dramatically impacts performance
Understand Loops
Master loop patterns = master complexity analysis