Skip to content

rope-50/javascript-algorithms

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

20 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

🧮 JavaScript Algorithms

Classic data structures and algorithms, implemented in clean, modern JavaScript - each one documented, complexity-annotated, and covered by tests.

CI Node Tested with Vitest License: MIT PRs welcome

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.

🚀 Getting started

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 report

Requires Node.js 20+.

📦 Usage

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]

📚 What's inside

Data structures

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

Sorting & 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

Math

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)

Strings

Algorithm Source Complexity
Reverse string reverseString.js O(n)
Permutation check stringPermutation.js O(n)
Ransom note ransomNote.js O(n)

Classic problems

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!)

🗂️ Project structure

src/      one module per algorithm or data structure
tests/    one Vitest spec per module (mirrors src/)

🤝 Contributing

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.

📄 License

Released under the MIT License.

About

My own implementations of classic data structures & algorithms in modern JavaScript, built as preparation for an Amazon interview, each documented, complexity-annotated, and Vitest-tested.

Topics

Resources

License

Contributing

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors