๐ Algorithm Complexity Cheat Sheet
A visual and practical overview for understanding algorithmic complexity (Big O notation).
Prepared for the course "Algorithms and Data Structures in C# (.NET)"
๐จโ๐ซ Dr. Stoyan Cheresharov, Plovdiv University โPaisii Hilendarskiโ
๐ง What Is Big O Notation?
Big O notation describes how the runtime or memory usage of an algorithm grows as the input size (n) increases.
The O stands for Order โ it represents the upper bound (worst-case growth rate).
๐ข Why Do We Use It?
Big O lets us compare algorithms independently of hardware or language.
It shows how fast an algorithmโs runtime grows when the problem size increases.
โ๏ธ Common Complexities
| Big-O | Name | Example | Meaning |
|---|---|---|---|
| O(1) | Constant | Access array element | Same time regardless of input size |
| O(log n) | Logarithmic | Binary Search | Work halves each step |
| O(n) | Linear | Simple loop | Work grows directly with input size |
| O(n log n) | Quasi-linear | Merge Sort | Typical for efficient sorting |
| O(nยฒ) | Quadratic | Nested loops | Slower as size doubles |
| O(2โฟ) | Exponential | Recursive Fibonacci | Work doubles each time n increases |
| O(n!) | Factorial | Permutations | Tests all possible orderings |
๐ป C# Examples
๐ฉ Constant Time โ O(1)
int x = arr[0]; // Accessing by index
๐ฆ Linear Time โ O(n)
for (int i = 0; i < arr.Length; i++)
Console.WriteLine(arr[i]);
๐ฅ Quadratic Time โ O(nยฒ)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++)
sum += i * j;
๐จ Logarithmic Time โ O(log n)
int BinarySearch(int[] arr, int x)
{
int low = 0, high = arr.Length - 1;
while (low <= high)
{
int mid = (low + high) / 2;
if (arr[mid] == x) return mid;
if (arr[mid] < x) low = mid + 1;
else high = mid - 1;
}
return -1;
}
๐ Growth Visualization (Conceptual)
Runtime growth vs Input size n
โ
โ O(nยฒ)
โ O(n log n)
โ O(n)
โ O(log n)
โ O(1)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ n
Interpretation:
- O(1) โ Instant (constant work)
- O(log n) โ Slower growth (binary search)
- O(n) โ Linear growth (single loop)
- O(nยฒ) โ Rapid growth (nested loops)
๐งฎ How Complexities Are Calculated
| Type | Example | Step Count | Result |
|---|---|---|---|
| Constant | arr[0] |
1 | O(1) |
| Linear | for (i < n) |
n | O(n) |
| Quadratic | for(i<n) for(j<n) |
n ร n | O(nยฒ) |
| Logarithmic | Binary search | logโ(n) | O(log n) |
| Quasi-linear | Merge sort | n ร logโ(n) | O(n log n) |
๐ง Key Insights
โ
We ignore constants: O(3n + 10) โ O(n)
โ
We keep only the dominant term: O(nยฒ + n) โ O(nยฒ)
โ
Big O describes growth trend, not actual seconds.
โ
Focus on asymptotic behavior โ as n โ โ.
๐งฉ Memory (Space) Complexity Examples
| Operation | Space Complexity |
|---|---|
| Simple variable | O(1) |
| Array of size n | O(n) |
| 2D matrix nรn | O(nยฒ) |
โ๏ธ Real-world Analogy
| Complexity | Analogy |
|---|---|
| O(1) | Teleport โ instant |
| O(log n) | Elevator โ halves distance each step |
| O(n) | Walking โ proportional to distance |
| O(n log n) | Walking uphill |
| O(nยฒ) | Searching every corner of every room |
| O(2โฟ) | Checking every possible combination |
| O(n!) | Reordering everything in every possible way |
๐ Quick Comparison
| Complexity | 10 items | 100 items | 1000 items |
|---|---|---|---|
| O(1) | 1 | 1 | 1 |
| O(log n) | 3 | 7 | 10 |
| O(n) | 10 | 100 | 1000 |
| O(n log n) | 30 | 700 | 10,000 |
| O(nยฒ) | 100 | 10,000 | 1,000,000 |
| O(2โฟ) | 1024 | huge | impossible |
| O(n!) | 3,628,800 | impossible | impossible |
๐งญ Summary
Big O is not about exact speed, itโs about how performance scales.
In short:
- Use O(1) when possible.
- Aim for O(n) or O(n log n) in practical algorithms.
- Avoid O(nยฒ) and worse in large data sets.
๐ Prepared by: Dr. Stoyan Cheresharov
๐ Plovdiv University โPaisii Hilendarskiโ
๐ฅ๏ธ Course: Algorithms and Data Structures in C# (.NET)