# javascript-algorithms | Oleksii Trekhleb | Knowledge Base | Instagit

📝 Algorithms and data structures implemented in JavaScript with explanations and links to further readings

GitHub Stars: 196k

Repository: https://github.com/trekhleb/javascript-algorithms

---

## Articles

### [Hill Cipher and Caesar Cipher Implementation in JavaScript](/trekhleb/javascript-algorithms/how-are-the-cryptographic-algorithms-hill-cipher-caesar-cipher-implemented)

Discover JavaScript implementations of Hill Cipher and Caesar Cipher. Learn how these cryptographic algorithms use modular arithmetic and matrix multiplication for encryption in this clear guide.

- Tags: deep-dive
- Published: 2026-02-24

### [N-Queens Problem Backtracking Approach in JavaScript: A Complete Implementation Guide](/trekhleb/javascript-algorithms/whats-the-implementation-of-the-n-queens-problem-backtracking-approach)

Learn the N-Queens problem backtracking approach in JavaScript. This guide details the recursive algorithm, safe queen placement, and conflict resolution for finding all solutions.

- Tags: how-to-guide
- Published: 2026-02-24

### [How the Recursive Staircase Solution Uses Dynamic Programming Memoization](/trekhleb/javascript-algorithms/how-does-the-recursive-staircase-solution-use-dynamic-programming-memoization)

Learn how the recursive staircase dynamic programming solution employs memoization to optimize performance. Slash complexity from O(2ⁿ) to O(n) by solving subproblems just once.

- Tags: deep-dive
- Published: 2026-02-24

### [How to Implement Bitwise Operations for Math Algorithms in JavaScript](/trekhleb/javascript-algorithms/whats-the-approach-for-implementing-bitwise-operations-for-math-algorithms)

Master bitwise operations in JavaScript for math algorithms. Learn to use core operators like & ^ and >>> to manipulate 32-bit integers effectively. Explore the trekhleb javascript-algorithms repo.

- Tags: how-to-guide
- Published: 2026-02-24

### [KMP vs Z-Algorithm vs Rabin-Karp: Comparing String Matching Algorithms in JavaScript](/trekhleb/javascript-algorithms/how-do-the-different-string-matching-algorithms-kmp-z-rabin-karp-compare)

Explore KMP vs Z-Algorithm vs Rabin-Karp string matching in JavaScript. Discover their preprocessing, space needs, and performance for efficient substring search.

- Tags: comparison
- Published: 2026-02-24

### [Strongly Connected Components (Kosaraju's Algorithm) Implementation in JavaScript](/trekhleb/javascript-algorithms/whats-the-implementation-of-the-strongly-connected-components-kosaraju-s)

Discover the JavaScript implementation of Kosaraju's algorithm for finding strongly connected components. Learn how two DFS passes and graph reversal achieve this decomposition.

- Tags: how-to-guide
- Published: 2026-02-24

### [How Does the Topological Sorting Algorithm Detect Cycles in Directed Graphs?](/trekhleb/javascript-algorithms/how-does-the-topological-sorting-algorithm-detect-cycles-in-directed-graphs)

Discover how the topological sort in javascript-algorithms handles directed graphs, noting its cycle detection limitations and how it avoids infinite recursion.

- Tags: deep-dive
- Published: 2026-02-24

### [BFS vs DFS Graph Traversal: Key Differences and When to Use Each](/trekhleb/javascript-algorithms/whats-the-difference-between-bfs-and-dfs-for-graph-traversal-use-cases)

Understand BFS vs DFS graph traversal. Learn how BFS finds shortest paths with a queue and DFS explores deeply with recursion for cycle detection and topological sorting.

- Tags: deep-dive
- Published: 2026-02-24

### [How the k-Means Clustering Algorithm Handles Centroid Initialization](/trekhleb/javascript-algorithms/how-does-the-k-means-clustering-algorithm-handle-centroid-initialization)

Discover how k-Means clustering algorithm selects initial centroids using the first k data points. Learn about effective centroid initialization in this article.

- Tags: deep-dive
- Published: 2026-02-24

### [How the Seam Carving Image Resizing Algorithm Is Implemented in trekhleb/javascript-algorithms](/trekhleb/javascript-algorithms/whats-the-implementation-approach-for-the-seam-carving-image-resizing-algorithm)

Explore the Seam Carving algorithm implementation in javascript-algorithms. Learn content-aware image resizing via energy maps, seam selection, and removal.

- Tags: how-to-guide
- Published: 2026-02-24

### [How Kruskal's and Prim's Algorithms Differ for MST: A Code-Based Comparison](/trekhleb/javascript-algorithms/how-are-graph-algorithms-like-kruskal-s-and-prim-s-different-for-mst)

Compare Kruskal's and Prim's MST algorithms side-by-side. Understand their code-based differences in edge sorting, cycle avoidance, and time complexity for finding minimum spanning trees.

- Tags: deep-dive
- Published: 2026-02-24

### [How Disjoint Set (Union-Find) Path Compression Works in JavaScript Algorithms](/trekhleb/javascript-algorithms/how-does-the-disjoint-set-union-find-structure-implement-path-compression)

Learn how Disjoint Set path compression optimizes Union-Find algorithms. Understand the technique and its JavaScript implementation details.

- Tags: deep-dive
- Published: 2026-02-24

### [0/1 Knapsack vs Unbounded Knapsack: Key Differences in JavaScript](/trekhleb/javascript-algorithms/whats-the-difference-between-0-1-knapsack-and-unbounded-knapsack-implementations)

Explore the core differences between 0/1 Knapsack and Unbounded Knapsack algorithms in JavaScript. Learn when to use dynamic programming for optimal solutions or a greedy approach for unlimited items.

- Tags: deep-dive
- Published: 2026-02-24

### [How the Trie Data Structure Optimizes Prefix and Autocomplete Searches](/trekhleb/javascript-algorithms/how-does-the-trie-data-structure-optimize-prefix-and-autocomplete-searches)

Discover how the Trie data structure optimizes prefix and autocomplete searches with O(k) lookup time. Learn to build efficient search functionalities.

- Tags: deep-dive
- Published: 2026-02-24

### [QuickSort vs MergeSort: When to Choose QuickSort for In-Place Efficiency](/trekhleb/javascript-algorithms/what-makes-quicksort-efficient-and-when-should-i-choose-it-over-mergesort)

QuickSort offers efficient in-place sorting with O(1) space. Learn when to choose QuickSort over MergeSort for optimal performance.

- Tags: comparison
- Published: 2026-02-24

### [How the JavaScript Algorithms Repository Structures Implementations by Paradigm](/trekhleb/javascript-algorithms/how-does-the-repository-structure-algorithm-implementations-by-paradigm)

Explore how the trekhleb javascript-algorithms repository structures implementations by paradigm like sorting graph dynamic-programming and greedy ensuring organized and accessible code.

- Tags: internals
- Published: 2026-02-24

### [How the Rabin-Karp Algorithm Uses Rolling Hash for String Matching](/trekhleb/javascript-algorithms/how-does-the-rabin-karp-algorithm-use-rolling-hash-for-string-matching)

Discover how the Rabin-Karp algorithm employs rolling hash for efficient string matching. Learn to accelerate substring search with constant-time hash updates and O(N + M) complexity.

- Tags: internals
- Published: 2026-02-24

### [Fibonacci Heap in the JavaScript-Algorithms Repository: Implementation Analysis](/trekhleb/javascript-algorithms/whats-the-internal-implementation-of-the-fibonacci-heap-optimization)

Explore the trekhleb javascript-algorithms repository for heap implementations. Discover why Fibonacci heap optimization is not present and what heap structures are available.

- Tags: internals
- Published: 2026-02-24

### [Dijkstra's Algorithm vs Bellman-Ford: Key Differences for Shortest-Path Computation](/trekhleb/javascript-algorithms/how-does-dijkstra-s-algorithm-differ-from-bellman-ford-for-shortest-paths)

Dijkstra's and Bellman-Ford algorithms find shortest paths. Discover their key differences in complexity and handling negative edge weights to choose the right algorithm.

- Tags: deep-dive
- Published: 2026-02-24

### [How Bloom Filters Handle False Positives and Their Trade-offs](/trekhleb/javascript-algorithms/how-does-the-bloom-filter-handle-false-positives-and-what-are-the-trade-offs)

Learn how Bloom filters manage false positives in this space-efficient probabilistic data structure. Understand the trade-offs for your projects.

- Tags: deep-dive
- Published: 2026-02-24

### [LRU Cache Implementation in JavaScript: O(1) Operations and Efficient Eviction](/trekhleb/javascript-algorithms/how-is-the-lru-cache-implemented-and-what-makes-it-efficient-for-eviction)

Learn how to implement LRU Cache in JavaScript with O(1) operations. Discover efficient eviction using a hash map and doubly-linked list for optimal performance.

- Tags: deep-dive
- Published: 2026-02-24

### [Time and Space Complexities of Red-Black Tree vs AVL Tree: A Complete Guide](/trekhleb/javascript-algorithms/what-are-the-time-and-space-complexities-of-red-black-tree-vs-avl-tree-operations)

Compare Red-Black tree vs AVL tree time and space complexities. Understand O(log n) for search, insert, delete. AVL offers faster lookups, Red-Black faster modifications. Master tree balancing

- Tags: deep-dive
- Published: 2026-02-24

### [How the Graph Data Structure Handles Directed vs Undirected Edges in JavaScript](/trekhleb/javascript-algorithms/how-does-the-graph-data-structure-handle-directed-vs-undirected-edges-internally)

Learn how the Graph data structure in javascript-algorithms handles directed vs undirected edges using an isDirected flag for seamless addEdge and getNeighbors functionality.

- Tags: internals
- Published: 2026-02-24

