1
Time and Space Complexity (Big-O)
★★★★★
↑ Index
Algorithm
Time complexity is how the running time grows with input size n. Space complexity is how much extra memory is used. We usually give the worst case with Big-O.
- Big-O (O) — upper bound (worst case).
- Big-Omega (Ω) — lower bound (best case).
- Big-Theta (Θ) — tight bound (both).

O(1) < O(log n) < O(n) < O(n log n) < O(n²) < O(2ⁿ)
Recognising complexity from code · C++
x = a[5]; // O(1)
for (int i = 1; i < n; i *= 2) {} // O(log n)
for (int i = 0; i < n; i++) {} // O(n)
for (int i = 0; i < n; i++)
for (int j = 0; j < n; j++) {} // O(n^2)
sort(a, a + n); // O(n log n)
Rule of thumb: about 108 simple operations run in ~1 second. For n = 105, O(n²) is too slow but O(n log n) is fine.
2
BFS and DFS
★★★★★
↑ Index
Algorithm
BFS (Breadth First Search) visits level by level with a queue; it finds the shortest path in an unweighted graph. DFS (Depth First Search) goes as deep as possible with a stack / recursion; used for cycle detection, topological sort, connected components. Both are O(V + E).

BFS spreads out, DFS dives down one branch first
BFS · C++
void bfs(int start, vector<vector<int>> &adj) {
vector<bool> visited(adj.size(), false);
queue<int> q;
visited[start] = true;
q.push(start);
while (!q.empty()) {
int u = q.front(); q.pop();
cout << u << " ";
for (int v : adj[u])
if (!visited[v]) { visited[v] = true; q.push(v); }
}
}
DFS (recursive) · C++
void dfs(int u, vector<vector<int>> &adj, vector<bool> &visited) {
visited[u] = true;
cout << u << " ";
for (int v : adj[u])
if (!visited[v]) dfs(v, adj, visited);
}
3
Dynamic Programming
★★★★★
↑ Index
Algorithm
DP solves a problem by breaking it into overlapping subproblems and storing each answer so it is computed only once. The problem must have optimal substructure.
- Memoization (top-down) — recursion + cache.
- Tabulation (bottom-up) — fill a table with loops.

Plain recursion recomputes f(3) and f(2) many times
0/1 Knapsack O(n × W) · C++
int knapsack(vector<int> &wt, vector<int> &val, int W) {
int n = wt.size();
vector<vector<int>> dp(n + 1, vector<int>(W + 1, 0));
for (int i = 1; i <= n; i++)
for (int w = 0; w <= W; w++) {
dp[i][w] = dp[i - 1][w]; // skip item
if (wt[i - 1] <= w)
dp[i][w] = max(dp[i][w], val[i - 1] + dp[i - 1][w - wt[i - 1]]); // take item
}
return dp[n][W];
}
Longest Common Subsequence · C++
int lcs(string a, string b) {
int n = a.size(), m = b.size();
vector<vector<int>> dp(n + 1, vector<int>(m + 1, 0));
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++)
dp[i][j] = (a[i-1] == b[j-1]) ? dp[i-1][j-1] + 1 : max(dp[i-1][j], dp[i][j-1]);
return dp[n][m]; // lcs("ABCBDAB", "BDCABA") = 4
}
Coin change · C++
int coinChange(vector<int> &coins, int amount) { // minimum coins
vector<int> dp(amount + 1, INT_MAX);
dp[0] = 0;
for (int a = 1; a <= amount; a++)
for (int c : coins)
if (c <= a && dp[a - c] != INT_MAX)
dp[a] = min(dp[a], dp[a - c] + 1);
return dp[amount] == INT_MAX ? -1 : dp[amount];
}
4
Binary Search
★★★★★
↑ Index
Algorithm › Search
Works only on a sorted array. Compare with the middle element and discard half of the range each time. Time O(log n), space O(1) (iterative).

Searching 23 takes only 3 steps for 10 elements
Iterative · C++
int binarySearch(int arr[], int n, int key) {
int low = 0, high = n - 1;
while (low <= high) {
int mid = low + (high - low) / 2; // avoids overflow of (low + high)
if (arr[mid] == key) return mid;
if (arr[mid] < key) low = mid + 1;
else high = mid - 1;
}
return -1;
}
Recursive (O(log n) stack space) · C++
int binarySearch(int arr[], int low, int high, int key) {
if (low > high) return -1;
int mid = low + (high - low) / 2;
if (arr[mid] == key) return mid;
if (arr[mid] < key) return binarySearch(arr, mid + 1, high, key);
return binarySearch(arr, low, mid - 1, key);
}
Q & A
- Why mid = low + (high - low) / 2?
low + high can overflow int when both are large; this form gives the same value safely.
6
Sorting Algorithms Comparison
★★★★★
↑ Index
Algorithm › Sort
| Algorithm | Best | Average | Worst | Space | Stable |
|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Counting Sort | O(n + k) | O(n + k) | O(n + k) | O(k) | Yes |
Stable = equal elements keep their original order. In-place = O(1) extra memory. C++ sort() uses Introsort (quick + heap + insertion), O(n log n).
7
Merge Sort
★★★★★
↑ Index
Algorithm › Sort
Divide and conquer: split the array into halves, sort each half recursively, then merge the two sorted halves. Always O(n log n), stable, needs O(n) extra space.

Split down to single elements, then merge back in order
C++
void merge(vector<int> &a, int l, int m, int r) {
vector<int> tmp;
int i = l, j = m + 1;
while (i <= m && j <= r)
tmp.push_back(a[i] <= a[j] ? a[i++] : a[j++]);
while (i <= m) tmp.push_back(a[i++]);
while (j <= r) tmp.push_back(a[j++]);
for (int k = 0; k < tmp.size(); k++) a[l + k] = tmp[k];
}
void mergeSort(vector<int> &a, int l, int r) {
if (l >= r) return;
int m = l + (r - l) / 2;
mergeSort(a, l, m);
mergeSort(a, m + 1, r);
merge(a, l, m, r);
}
Recurrence: T(n) = 2T(n/2) + O(n) → O(n log n).
8
Quick Sort
★★★★★
↑ Index
Algorithm › Sort
Pick a pivot, partition the array so smaller elements are on the left and larger on the right, then sort both sides recursively. Average O(n log n), in-place. Worst case O(n²) when the pivot is always the smallest/largest (e.g. already sorted input with last-element pivot); a random pivot avoids it.

After one partition the pivot is at its final index
C++
int partition(vector<int> &a, int low, int high) { // Lomuto
int pivot = a[high];
int i = low - 1;
for (int j = low; j < high; j++)
if (a[j] < pivot) swap(a[++i], a[j]);
swap(a[i + 1], a[high]);
return i + 1;
}
void quickSort(vector<int> &a, int low, int high) {
if (low < high) {
int p = partition(a, low, high);
quickSort(a, low, p - 1);
quickSort(a, p + 1, high);
}
}
Q & A
- Merge sort vs quick sort?
Merge sort: always O(n log n), stable, needs O(n) extra memory, good for linked lists and external sorting. Quick sort: in-place and usually faster in practice, but O(n²) in the worst case and not stable.