In computer science, algorithmic efficiency is not measured in seconds or clock cycles, because physical execution time varies wildly across different hardware, operating systems, and CPU loads. Instead, we measure how the execution time or memory space scales as input size (n) tends toward infinity.
1. Mathematical Definitions: Big-O, Big-(Omega), and Big-(Theta)
Formally, asymptotic notations characterize the bounding functions of an algorithm's growth rate:
Big-O ((mathcal{O}))
Asymptotic Upper Bound
Represents the worst-case performance. The algorithm will never perform worse than this curve.
Big-(Omega) ((Omega))
Asymptotic Lower Bound
Represents the best-case performance. The algorithm will never run faster than this bound.
Big-(Theta) ((Theta))
Asymptotically Tight Bound
Represents an exact bound when upper and lower bounds share identical growth orders.
2. Common Time Complexity Classes Ranked
From fastest to slowest growth orders as (n) increases:
| Complexity | Name | Example Operations & Algorithms | Scalability Verdict |
|---|---|---|---|
| (mathcal{O}(1)) | Constant Time | Array index lookup, Hash table key retrieval, Stack push/pop | Excellent |
| (mathcal{O}(log n)) | Logarithmic Time | Binary Search, Balanced Binary Search Tree lookup | Superb |
| (mathcal{O}(n)) | Linear Time | Linear search across array, traversing linked list, finding min/max | Fair |
| (mathcal{O}(n log n)) | Linearithmic | MergeSort, HeapSort, QuickSort (average case) | Good |
| (mathcal{O}(n^2)) | Quadratic Time | BubbleSort, InsertionSort, nested iterations over data | Poor |
| (mathcal{O}(2^n)) | Exponential Time | Recursive Fibonacci, subset generation, brute-force traveling salesperson | Intolerable |
| (mathcal{O}(n!)) | Factorial Time | Generating all permutations of an array | Impractical |
3. How to Calculate Complexity: Step-by-Step Example
Let's analyze the time complexity of the Binary Search algorithm:
def binary_search(arr: list[int], target: int) -> int:
low = 0
high = len(arr) - 1
while low <= high:
mid = (low + high) // 2
if arr[mid] == target:
return mid # Target found at index
elif arr[mid] < target:
low = mid + 1
else:
high = mid - 1
return -1 # Target not found
Recurrence Relation Derivation
In every iteration of the loop, the remaining search window is halved: (frac{n}{2}, frac{n}{4}, frac{n}{8}, dots, frac{n}{2^k} = 1).
Solving for the number of steps (k):
Hence, the time complexity is strictly bounded by (mathcal{O}(log n)).
The step-by-step derivation of Binary Search complexity with KaTeX math is crystal clear. Thanks for writing this!