Common Big O Complexities
| Big O | Name | Typical example | Growth intuition |
|---|---|---|---|
| O(1) | Constant | Array lookup by index | Cost stays about the same as n grows |
| O(log n) | Logarithmic | Binary search | Doubling n adds only a small extra cost |
| O(n) | Linear | Linear search | Cost grows in direct proportion to n |
| O(n log n) | Log-linear | Merge sort, quicksort (average) | Efficient for large sorting tasks |
| O(n²) | Quadratic | Bubble sort, insertion sort (worst/avg) | Cost grows rapidly as n increases |
| O(2ⁿ) | Exponential | Brute-force recursion | Explodes quickly; impractical at scale |
Time vs Space Complexity
| Complexity type | What it measures | Typical trade-off |
|---|---|---|
| Time complexity | How runtime grows with input size n | Faster runtime may need extra memory |
| Space complexity | How extra memory use grows with n | Lower memory usage may increase runtime |
Working Out the Complexity of an Algorithm
| What the algorithm does | Complexity | Reason |
|---|---|---|
| A fixed number of steps with no loop over the data | O(1), constant | The number of steps does not depend on n |
| Halves the data still to be dealt with on each pass | O(log n), logarithmic | Doubling n adds only one more pass |
| One loop that visits each item once | O(n), linear | n items need n passes |
| One loop over the data nested inside another | O(n²), polynomial | The inner loop makes n passes for each of the n passes of the outer loop |
| Tries every combination, so each extra item doubles the work | O(2ⁿ), exponential | The number of steps doubles for every item added |
for i = 0 to n - 1:
for j = 0 to n - 1:
if i != j AND items[i] == items[j] then
print("Duplicate found")
end if
end for
end for
Best, average and worst case can differ for the same algorithm. A linear search is O(1) in the best case, when the target is the first item, and O(n) in the worst case, when the target is last or not in the list.