Sorting, searching, trees, linked lists, matrices — visual explanations with Python code and complexity analysis.
From simple O(n²) to efficient O(n log n) — know when to use each
| Algorithm | Best | Average | Worst | Space | Stable? | When to Use |
|---|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Teaching only |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No | Minimal swaps needed |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes | Small/nearly sorted arrays |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes | Guaranteed O(n log n), stability needed |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No | General purpose, best avg performance |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No | In-place + guaranteed O(n log n) |
| Counting Sort | O(n + k) | O(n + k) | O(n + k) | O(k) | Yes | Small integer range (k) |
Finding elements efficiently — from linear to binary and beyond
Node-based data structures — master the pointer manipulation patterns
Binary trees, BST, traversals, and common patterns
Grid traversal, rotation, spiral, and search patterns
Know these cold — interviewers always ask about complexity
| Data Structure | Access | Search | Insert | Delete | Space |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | O(n) |
| Hash Table | N/A | O(1)* | O(1)* | O(1)* | O(n) |
| Linked List | O(n) | O(n) | O(1)† | O(1)† | O(n) |
| Stack / Queue | O(n) | O(n) | O(1) | O(1) | O(n) |
| BST (balanced) | O(log n) | O(log n) | O(log n) | O(log n) | O(n) |
| Heap | O(n) | O(n) | O(log n) | O(log n) | O(n) |
* average case, O(n) worst with collisions † O(1) if you have the node reference
| Complexity | Name | Example | n=1000 | n=1M |
|---|---|---|---|---|
| O(1) | Constant | Hash lookup, array index | 1 | 1 |
| O(log n) | Logarithmic | Binary search | 10 | 20 |
| O(n) | Linear | Single loop, hash map build | 1K | 1M |
| O(n log n) | Linearithmic | Merge sort, quick sort | 10K | 20M |
| O(n²) | Quadratic | Nested loops, bubble sort | 1M | 1T |
| O(2ⁿ) | Exponential | Recursion w/o memo | Huge | Heat death |