
Your code works perfectly…
But suddenly becomes painfully slow when data increases
Why does this happen?
The answer is Time Complexity
What is Time Complexity?
Time complexity measures how fast your algorithm grows with input size.
Simple question:
“If input doubles… how much slower will your code become?”
Big O Notation
We use Big O to describe worst-case performance.
It ignores constants and focuses on growth:
- O(2n) = O(n)
- O(n² + n) = O(n²)
Common Time Complexities (Best → Worst)
- O(1) – Constant (fastest)
- O(log n) – Very fast (Binary Search)
- O(n) – Linear
- O(n log n) – Efficient sorting
- O(n²) – Slow (nested loops)
- O(2ⁿ) – Extremely slow
Examples
O(1)
int first = arr[0];
O(n)
for (int i = 0; i < n; i++) { }
O(n²)
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) { }
}
Reality Check
Small input → difference doesn’t matter
Big input →
O(n²) can be 1000x slower than O(n)
Fore More: https://www.quipoin.com/tutorial/data-structure-with-java/time-complexity
Top comments (0)