$ show trekhleb/javascript-algorithms
This repository is a comprehensive, educational collection of classic algorithms and data structures implemented in JavaScript. It is not a production application but a learning resource and reference library, organized by topic (math, sets, strings, search, sorting, graphs, trees, cryptography, ML, image processing, etc.) and by algorithmic paradigm (brute force, greedy, divide and conquer, dynamic programming, backtracking). Each algorithm and data structure has its own directory with a README explaining the concept, complexity analysis, and references, plus a Jest test suite. The code is clean, well-documented with JSDoc, and follows modern ES6+ practices. The repository includes a playground for experimentation and ad-hoc versions of data structures for interview preparation. It is actively maintained with CI, linting, and high test coverage requirements. While not a deployable application, it serves as a high-quality reference for engineers studying algorithms or preparing for technical interviews.
The LLM identified 5 files worth highlighting for reuse:
A generic comparator class that provides flexible comparison logic (equality, less-than, greater-than, etc.) using a custom compare function or default JavaScript comparison. It is used by many data structures (LinkedList, Heap, etc.) and sorting algorithms, and can be extracted and reused in any project requiring customizable ordering.
Implements a polynomial rolling hash function for strings, with configurable base and modulus. Demonstrates the rolling hash technique used in substring search (Rabin-Karp) and plagiarism detection. The implementation is self-contained, handles large numbers via modular arithmetic, and can be reused in any string processing project.
Solves the 0/1 knapsack problem using dynamic programming. The implementation is clean, with a DP table and backtracking to find selected items. It is self-contained, well-tested, and can be extracted for any resource allocation or optimization problem.
Implements an LRU (Least Recently Used) cache using a doubly linked list and a hash map for O(1) operations. The code is self-contained, demonstrates a classic design pattern, and can be reused in any application requiring cache eviction.
Implements a Fenwick Tree (Binary Indexed Tree) for efficient prefix sum queries and point updates in O(log n). The implementation is concise, well-documented, and can be extracted for any problem requiring range sum queries with updates.