Time/Space Complexity · Time & Space Complexity
Time & Space Complexity
What is Time Complexity?
Time complexity measures how efficient an algorithm is as the input size increases. It's not the same as the actual time taken to run a program: Time Complexity != Execution Time.
Linear vs Binary Search
Linear Search
- Best Case:
- Element at 1st index → 1 operation
- Average Case:
- Element at n/2 index → n/2 operations
- Worst Case:
- Element not found → n operations
- Time Complexity:
O(n)- Requirement:
- Can work on unsorted arrays
- n = 10 → 10 ops
- n = 100 → 100 ops
- n = 1000 → 1000 ops
Binary Search
- Best Case:
- Middle element matched → 1 operation
- Average Case:
- log₂(n) operations
- Worst Case:
- log₂(n) operations
- Time Complexity:
O(log n)- Requirement:
- Only works on sorted arrays
- n = 10 → 4 ops
- n = 100 → 7 ops
- n = 1000 → 10 ops
When we use Linear Search on an input of size 100, it can take 100 steps, whereas Binary Search takes at most 7. As the input size (n) grows, the way an algorithm behaves tells us how efficient it is - and the curves show Binary Search pulling further ahead as n increases.
Big O Notation
Big O isn't a formula to calculate - it's a notation for the upper bound of an algorithm's growth. We use it to describe the worst-case complexity, ignoring constants and smaller terms.
Code Examples of Time Complexity
O(1) - Constant time
// Accessing the element at index 5
let value = arr[5];The time complexity is O(1) because we directly access index 5 without any iteration.
O(n) - Linear time
for (let i = 0; i < n; i++) {
// do something
}O(log n) - Logarithmic time
// e.g., Binary Search
function binarySearch(arr, key) {
let low = 0, high = arr.length - 1;
while (low <= high) {
let mid = Math.floor((low + high) / 2);
if (arr[mid] === key) return mid;
else if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}O(n^2) - Nested loop
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
// do something
}
}O(n log n) - A halving loop inside a linear loop
for (let i = 0; i < n; i++) {
let temp = n;
while (temp > 1) {
temp = Math.floor(temp / 2);
// do something
}
}O(n^3) - Triple nested loops
for (let i = 0; i < n; i++) {
for (let j = 0; j < n; j++) {
for (let k = 0; k < n; k++) {
// do something
}
}
}O(2^n) - Exponential time
// Recursive Fibonacci
function fib(n) {
if (n <= 1) return n;
return fib(n - 1) + fib(n - 2);
}O(n!) - Factorial time
// Permutation generator
function permute(chars, l, r) {
if (l === r) {
console.log(chars.join(""));
} else {
for (let i = l; i <= r; i++) {
[chars[l], chars[i]] = [chars[i], chars[l]];
permute(chars, l + 1, r);
[chars[l], chars[i]] = [chars[i], chars[l]]; // backtrack
}
}
}Time Complexity Priorities
From most to least efficient:
O(1)- Constant timeO(log n)- e.g., Binary SearchO(n)- e.g., Linear SearchO(n log n)- e.g., Merge SortO(n^2)- e.g., Nested LoopsO(n^3)- e.g., Triple Nested LoopsO(2^n)- Recursion (e.g., Fibonacci)O(n!)- e.g., Brute-force permutations
What is Space Complexity?
Space complexity refers to how much extra memory an algorithm uses. Examples:
- Access the 5th element:
O(1) - Find the max with a single variable:
O(1) - Create a new array of size n:
O(n) - Create an n × n 2D matrix:
O(n^2)