Classic data structures and algorithms, implemented in clean, modern JavaScript - each one documented, complexity-annotated, and covered by tests.
A study-and-reference collection of well-known algorithms and data structures. Every module is a small, self-contained ES module with:
- 📖 a docstring explaining the idea and its time/space complexity,
- 🧩 clear, commented code that favors readability over cleverness,
- ✅ a matching Vitest test that doubles as usage examples.
Great for interview prep, brushing up on fundamentals, or as a base to contribute your own.
git clone https://github.com/rope-50/javascript-algorithms.git
cd javascript-algorithms
npm install
npm test # run the whole suite once
npm run test:watch # re-run on file changes
npm run coverage # run with a coverage reportRequires Node.js 20+.
Everything is exported from src/, individually or via the barrel src/index.js:
import { mergeSort, BinarySearchTree, runningMedian } from './src/index.js';
mergeSort([5, 2, 9, 1]); // -> [1, 2, 5, 9]
new BinarySearchTree()
.insertAll([5, 3, 8, 1])
.inOrder(); // -> [1, 3, 5, 8]
runningMedian([12, 4, 5, 3]); // -> [12, 8, 5, 4.5]| Structure | Source | Highlights |
|---|---|---|
| Binary heap (min/max) | heap.js |
push/pop O(log n), custom comparator |
| Queue (two stacks) | queue.js |
FIFO with O(1) amortized ops |
| Binary search tree | binarySearchTree.js |
insert / delete / search / traversals |
| Trie (prefix tree) | trie.js |
word lookup + prefix counting |
| Algorithm | Source | Complexity |
|---|---|---|
| Merge sort | mergeSort.js |
O(n log n) |
| Count inversions | mergeSort.js |
O(n log n) |
| Running median | runningMedian.js |
O(log n) / element |
| Algorithm | Source | Complexity |
|---|---|---|
| Factorial | factorial.js |
O(n) |
| Primality test | primes.js |
O(√n) |
| Sieve of Eratosthenes | primes.js |
O(n log log n) |
| Polynomial derivative & integral | polynomial.js |
O(n) |
| Algorithm | Source | Complexity |
|---|---|---|
| Reverse string | reverseString.js |
O(n) |
| Permutation check | stringPermutation.js |
O(n) |
| Ransom note | ransomNote.js |
O(n) |
| Problem | Source | Complexity |
|---|---|---|
| Coin change (count ways) | coinChange.js |
O(amount × coins) |
| H-index | hIndex.js |
O(n) |
| Celebrity problem | celebrity.js |
O(n) |
| Permutations | permutations.js |
O(n · n!) |
src/ one module per algorithm or data structure
tests/ one Vitest spec per module (mirrors src/)
Contributions are very welcome - whether it's a brand-new algorithm, a clearer explanation, an extra test, or a bug fix. New to open source? This is a friendly place to make your first PR. 💚
Have a look at CONTRIBUTING.md for the (short) guidelines, then open an issue or a pull request. Every addition just needs a docstring with its complexity and a test.
Released under the MIT License.