用 JavaScript 实现的算法与数据结构 - 附详细解释与学习链接
⭐ 195,821 Stars GitHub 仓库 JavaScript MIT LicenseJavaScript Algorithms and Data Structures 是由 Oleksii Trekhleb 创建的开源项目,包含了大量使用 JavaScript 实现的经典算法和数据结构。每个算法和数据结构都有独立的 README 文件,提供相关解释和进一步阅读的链接(包括 YouTube 视频)。
项目中的算法按难度分为初级 (B) 和高级 (A) 两个等级,方便学习者根据自身水平选择。该仓库还提供了多种语言的翻译版本,包括简体中文。
| 项目信息 | 详情 |
|---|---|
| GitHub 星标 | 195,821+ |
| 作者 | Oleksii Trekhleb (@trekhleb) |
| 编程语言 | JavaScript |
| 数据结构 | 15+ 种 |
| 算法实现 | 100+ 个 |
| 开源许可 | MIT License |
| 多语言支持 | 简体中文、繁体中文、日语、韩语等 17 种语言 |
项目涵盖了所有主要的数据结构实现,每个都附有清晰的代码和详细的说明:
链表 - 单向链表和双向链表的完整实现,包含遍历、插入、删除等操作。
栈 (Stack) 和 队列 (Queue) - 经典的 LIFO 和 FIFO 数据结构,以及优先队列。
Hash Table - 基于键值对的高效数据存储,理想情况下 O(1) 的查找性能。
Heap - 最大堆和最小堆的实现,支持优先队列操作。
二叉搜索树、AVL 树、红黑树、线段树、Fenwick 树 - 各类树结构及其平衡维护算法。
图 (Graph)、前缀树 (Trie)、并查集、布隆过滤器、LRU 缓存。
| 数据结构 | 访问 | 搜索 | 插入 | 删除 |
|---|---|---|---|---|
| 数组 (Array) | O(1) | O(n) | O(n) | O(n) |
| 栈 (Stack) | O(n) | O(n) | O(1) | O(1) |
| 队列 (Queue) | O(n) | O(n) | O(1) | O(1) |
| 链表 (Linked List) | O(n) | O(n) | O(1) | O(n) |
| 哈希表 (Hash Table) | - | O(n) | O(n) | O(n) |
| 二叉搜索树 (BST) | O(n) | O(n) | O(n) | O(n) |
| 红黑树 | O(log n) | O(log n) | O(log n) | O(log n) |
| AVL 树 | O(log n) | O(log n) | O(log n) | O(log n) |
| 布隆过滤器 | - | O(1) | O(1) | - |
项目中的算法按主题分类组织,涵盖以下领域:
位运算、阶乘、斐波那契数列、素数检测、欧几里得算法、帕斯卡三角、快速幂、傅里叶变换等。
笛卡尔积、Fisher-Yates 洗牌、幂集、排列组合、最长公共子序列、背包问题、最大子数组。
汉明距离、回文检测、Levenshtein 距离、KMP 算法、Z 算法、Rabin-Karp、正则匹配。
线性搜索、跳跃搜索、二分搜索、插值搜索。
冒泡排序、选择排序、插入排序、堆排序、归并排序、快速排序、希尔排序、计数排序、基数排序、桶排序。
DFS、BFS、Kruskal、Dijkstra、Bellman-Ford、Floyd-Warshall、Prim、拓扑排序、强连通分量、旅行商问题。
多项式哈希、栅栏密码、凯撒密码、希尔密码。
NanoNeuron、KNN、K-Means、Seam Carving 图像处理、加权随机、遗传算法。
项目还按照算法设计范式对算法进行了分类,帮助你理解不同的问题解决策略:
| 范式 | 核心思想 | 典型算法 |
|---|---|---|
| 暴力法 (Brute Force) | 穷举所有可能性,选择最优解 | 线性搜索、旅行商问题、最大子数组、离散傅里叶变换 |
| 贪心法 (Greedy) | 每步选择当前最优,不考虑未来 | 跳跃游戏、Dijkstra、Prim、Kruskal |
| 分治法 (Divide and Conquer) | 将问题分解为更小的子问题分别解决 | 二分搜索、归并排序、快速排序、汉诺塔、快速幂 |
| 动态规划 (Dynamic Programming) | 利用已求解的子问题构建最终解 | 斐波那契、Levenshtein 距离、LCS、背包问题、Floyd-Warshall |
| 回溯法 (Backtracking) | 生成可能解,不满足条件时回退 | 幂集、排列组合、N 皇后、骑士巡游、组合求和 |
| 分支限界 (Branch & Bound) | 在回溯基础上用下界剪枝提升效率 | 结合 BFS 和 DFS 遍历状态空间树的高级优化方法 |
你可以在 src/playground/playground.js 中编写和测试自己的算法代码:
| 算法 | 最佳 | 平均 | 最差 | 空间 | 稳定 |
|---|---|---|---|---|---|
| 冒泡排序 | O(n) | O(n2) | O(n2) | O(1) | 是 |
| 插入排序 | O(n) | O(n2) | O(n2) | O(1) | 是 |
| 选择排序 | O(n2) | O(n2) | O(n2) | O(1) | 否 |
| 堆排序 | O(n log n) | O(n log n) | O(n log n) | O(1) | 否 |
| 归并排序 | O(n log n) | O(n log n) | O(n log n) | O(n) | 是 |
| 快速排序 | O(n log n) | O(n log n) | O(n2) | O(log n) | 否 |
| 希尔排序 | O(n log n) | 取决于间隔序列 | O(n(log n)2) | O(1) | 否 |
| 计数排序 | O(n+r) | O(n+r) | O(n+r) | O(n+r) | 是 |
| 基数排序 | O(nk) | O(nk) | O(nk) | O(n+k) | 是 |
| 表示法 | 类型 | 10 个元素 | 100 个元素 | 1000 个元素 |
|---|---|---|---|---|
| O(1) | 常数 | 1 | 1 | 1 |
| O(log N) | 对数 | 3 | 6 | 9 |
| O(N) | 线性 | 10 | 100 | 1000 |
| O(N log N) | 线性对数 | 30 | 600 | 9000 |
| O(N2) | 二次方 | 100 | 10,000 | 1,000,000 |
| O(2N) | 指数 | 1,024 | 1.26e+29 | 1.07e+301 |
| O(N!) | 阶乘 | 3,628,800 | 9.3e+157 | 4.02e+2567 |
以下是该项目当前开放的 Issue(按评论数排序),共 92 条,标题已翻译为中文供参考。
| 编号 | 议题标题(中文翻译 / 英文原文) | 创建日期 | 评论 |
|---|---|---|---|
| #1008 |
Factorial of a negative number does not exist
Factorial of a negative number does not exist |
2023-03-17 | 18 |
| #89 |
Translate to russian
Translate to russian 增强 |
2018-07-03 | 13 |
| #990 |
The "i" of the for loop can be initialized with the value 2
The "i" of the for loop can be initialized with the value 2 |
2023-01-31 | 10 |
| #568 |
Linear 搜索 修复
Linear Search Fix |
2020-10-17 | 10 |
| #339 |
Cartesian product with an empty set 应该 be an empty set
Cartesian product with an empty set should be an empty set |
2019-04-15 | 9 |
| #196 |
Suggestion: ship production ready 构建 to npm
Suggestion: ship production ready build to npm 增强 |
2018-09-02 | 9 |
| #12 |
A* algorithm?
A* algorithm? 增强 |
2018-05-24 | 9 |
| #960 |
Eu realmente não entendi
Eu realmente não entendi |
2022-11-17 | 7 |
| #529 |
Inefficient Hash Table 实现
Inefficient Hash Table implementation |
2020-08-20 | 7 |
| #2085 |
zh-TW translations 缺失 — 应该 contributors help complete?
zh-TW translations missing — should contributors help complete? |
2025-11-08 | 6 |
| #1154 |
Least Common Multiple for a array of elements.
Least Common Multiple for a array of elements. |
2024-07-20 | 6 |
| #1048 |
NO code available for Longest Increasing Subsequence
NO code available for Longest Increasing Subsequence |
2023-06-28 | 6 |
| #566 |
添加 MiniMax
Add MiniMax |
2020-10-17 | 6 |
| #248 |
Knapsack has 错误
Knapsack has error 缺陷 |
2018-11-16 | 6 |
| #1030 |
I don't see kadane's algorithm for maximum subarray sum
I don't see kadane's algorithm for maximum subarray sum |
2023-04-16 | 5 |
| #880 |
[Typo] README.md
[Typo] README.md |
2022-04-19 | 5 |
| #1106 |
为什么 this pointing to global context is giving undefined, even when global variable is defined ?
Why this pointing to global context is giving undefined, even when global variable is defined ? |
2024-02-22 | 4 |
| #1092 |
Traverse in Reverse is unnecessary complex
Traverse in Reverse is unnecessary complex |
2023-12-10 | 4 |
| #1070 |
priorityQueue does not poll out as expected
priorityQueue does not poll out as expected |
2023-09-06 | 4 |
| #939 |
read-black-tree
read-black-tree |
2022-09-26 | 4 |
| #889 |
`isPowerOfTwo` algos don't specify their ranges
`isPowerOfTwo` algos don't specify their ranges |
2022-05-29 | 4 |
| #827 |
实现 new Data Structure: SkipList
Implement new Data Structure: SkipList |
2021-12-30 | 4 |
| #640 |
Wrong array representation in Heap README.md
Wrong array representation in Heap README.md |
2021-02-13 | 4 |
| #406 |
添加 more cryptography
Add more cryptography |
2019-10-08 | 4 |
| #272 |
Provide use cases for the abstraction
Provide use cases for the abstraction 增强 |
2018-12-22 | 4 |
| #119 |
My concern about no classification by Procedural/Imperative and Functional programming Paradigm
My concern about no classification by Procedural/Imperative and Functional programming Paradigm 增强 |
2018-07-29 | 4 |
| #2046 |
Optimize Caesar Cipher 实现 and Enhance Robustness
Optimize Caesar Cipher Implementation and Enhance Robustness |
2025-03-13 | 3 |
| #1064 |
Binary 搜索 文件, comment typo
Binary Search file, comment typo |
2023-08-27 | 3 |
| #1034 |
Translation to Khmer
Translation to Khmer |
2023-05-01 | 3 |
| #1033 |
Small code 错误 for the LinkedList and HashTable data structures
Small code errors for the LinkedList and HashTable data structures |
2023-04-20 | 3 |
| #1016 |
缺陷(链接-列表): `insert` method duplicates value of head in tail
bug(linked-list): `insert` method duplicates value of head in tail |
2023-04-05 | 3 |
| #958 |
code translation
code translation |
2022-11-11 | 3 |
| #748 |
Pancake sort
Pancake sort |
2021-08-06 | 3 |
| #675 |
图片 are not visible in dark mode
images are not visible in dark mode |
2021-03-30 | 3 |
| #560 |
How can I use this lib with npm or yarn?
How can I use this lib with npm or yarn? |
2020-10-07 | 3 |
| #361 |
Linear Sort Algorithm?
Linear Sort Algorithm? |
2019-06-08 | 3 |
| #314 |
可以 we have just one way of doing the swapping to have better consistency across the code
Could we have just one way of doing the swapping to have better consistency across the code 增强 |
2019-02-19 | 3 |
| #310 |
BinarySearchTree can't 移除 a root node correctly
BinarySearchTree can't remove a root node correctly |
2019-02-16 | 3 |
| #252 |
detect-cycle-algorithms detect just one cycle
detect-cycle-algorithms detect just one cycle 增强 |
2018-11-23 | 3 |
| #206 |
添加 bucket sort
Add bucket sort 增强 |
2018-09-11 | 3 |
| #2048 |
功能请求: 添加 TypeScript Type Definitions
Feature Request: Add TypeScript Type Definitions |
2025-04-26 | 2 |
| #2043 |
Inconsistent 文件 Naming: CamelCase vs PascalCase
Inconsistent File Naming: CamelCase vs PascalCase |
2025-02-26 | 2 |
| #2038 |
= document.getElementById('todo-列表');
= document.getElementById('todo-list'); |
2025-01-17 | 2 |
| #1046 |
Translation of documents to Urdu, Hindi & Roman-Urdu
Translation of documents to Urdu, Hindi & Roman-Urdu |
2023-06-26 | 2 |
| #955 |
Eslint parsing 错误
Eslint parsing error |
2022-10-30 | 2 |
| #881 |
Quick sort in place reference 链接 takes me to weird site
Quick sort in place reference link takes me to weird site |
2022-04-24 | 2 |
| #873 |
[缺陷] Strongly connected 组件 function breaks when 添加 edges on opposite directions
[BUG] Strongly connected components function breaks when adding edges on opposite directions |
2022-03-21 | 2 |
| #858 |
npm该更新了 它老了呀
npm该更新了 它老了呀 |
2022-02-09 | 2 |
| #857 |
Data structures
Data structures |
2022-02-07 | 3 |
| #835 |
Inserting, removing, accessing, modifying, by index, on 链接 列表
Inserting, removing, accessing, modifying, by index, on linked list |
2022-01-16 | 2 |
| #830 |
Provide possible paths from A to B in a directed graph
Provide possible paths from A to B in a directed graph |
2022-01-09 | 2 |
| #656 |
Hard to view some 图片 or diagrams in GitHub's Dark Mode
Hard to view some images or diagrams in GitHub's Dark Mode |
2021-02-24 | 2 |
| #567 |
Optimize insertion sort
Optimize insertion sort |
2020-10-17 | 2 |
| #527 |
Creat A site web for both phone and pc
Creat A site web for both phone and pc |
2020-08-11 | 2 |
| #488 |
Sorting Visualization
Sorting Visualization |
2020-05-14 | 2 |
| #378 |
Redundant condition
Redundant condition |
2019-08-05 | 2 |
| #346 |
添加 fibonacci 搜索 algorithm
Add fibonacci search algorithm |
2019-04-24 | 2 |
| #325 |
缺陷 in AVL Tree rorateLeftLeft and rotateRightRight
Bug in AVL Tree rorateLeftLeft and rotateRightRight |
2019-03-14 | 2 |
| #323 |
Find cycles result is wrong!
Find cycles result is wrong! |
2019-03-13 | 2 |
| #271 |
添加 Splay Tree
Add Splay Tree 增强 |
2018-12-21 | 2 |
| #265 |
QuickSort : Instead of taking pivot as 1st element, we 应该 take the pivot as last element, due to time complexity
QuickSort : Instead of taking pivot as 1st element, we should take the pivot as last element, due to time complexity 增强 |
2018-12-09 | 2 |
| #234 |
添加 Bogosort
Add Bogosort 增强 |
2018-10-25 | 2 |
| #232 |
Luhn algorithm
Luhn algorithm 增强 |
2018-10-22 | 2 |
| #202 |
添加 TimSort
Add TimSort 增强 |
2018-09-04 | 2 |
| #44 |
添加 B-Tree
Add B-Tree 增强 |
2018-06-02 | 2 |
| #7 |
What about rope?
What about rope? 增强 |
2018-05-23 | 2 |
| #2065 |
LinkedList methods.
LinkedList methods. |
2025-08-04 | 2 |
| #1205 |
提案 to 添加 Secure Hash Algorithm ( SHA-1 ) Hashing Algorithm
Proposal to Add Secure Hash Algorithm ( SHA-1 ) Hashing Algorithm |
2024-10-21 | 1 |
| #1131 |
添加 Circular 链接 列表 In Data Structure.
Adding Circular Linked List In Data Structure. |
2024-05-12 | 1 |
| #1107 |
is there 支持 for 支持 for `BigInt`
is there support for support for `BigInt` |
2024-02-23 | 1 |
| #1103 |
Pendekatan "Brute Force" dan "Divide and Conquer"
Pendekatan "Brute Force" dan "Divide and Conquer" |
2024-02-09 | 1 |
| #1102 |
`replaceChild` method in BinaryTreeNode is not correct
`replaceChild` method in BinaryTreeNode is not correct |
2024-01-31 | 1 |
| #1095 |
Unhandled Edge Case in Binary 搜索 实现
Unhandled Edge Case in Binary Search Implementation |
2023-12-31 | 1 |
| #1085 |
会 be great to see `discussions` tab brought to this repository
Would be great to see `discussions` tab brought to this repository |
2023-11-16 | 1 |
| #1061 |
添加 Animations to Algorithms
Add Animations to Algorithms |
2023-08-24 | 1 |
| #1004 |
🐛 需要 code modification for insert method in LinkList code
🐛 Needed code modification for insert method in LinkList code |
2023-03-04 | 1 |
| #931 |
添加 CombSort
Add CombSort |
2022-08-27 | 1 |
| #872 |
npm package(s)
npm package(s) |
2022-03-07 | 1 |
| #868 |
Your project has been 添加 to a 列表 of copyfree works.
Your project has been added to a list of copyfree works. |
2022-02-25 | 1 |
| #867 |
添加 Markov Chain or Markov Process algorithm
Add Markov Chain or Markov Process algorithm |
2022-02-25 | 1 |
| #866 |
pictures in algo and data structures not visible on the 默认 github dark theme
pictures in algo and data structures not visible on the default github dark theme |
2022-02-22 | 1 |
| #692 |
Bit Reversal 缺失?
Bit Reversal missing? |
2021-04-17 | 1 |
| #671 |
错误 while condition in Simplified Chinese Code
error while condition in Simplified Chinese Code |
2021-03-29 | 1 |
| #655 |
Maybe there are some redundent code in Doubly-链接-列表
Maybe there are some redundent code in Doubly-Linked-List |
2021-02-24 | 1 |
| #583 |
链接 列表: Define a Cycle
Linked List: Define a Cycle |
2020-12-02 | 1 |
| #555 |
Stable Matching Algorithm
Stable Matching Algorithm |
2020-10-01 | 1 |
| #546 |
More detailed instructions on 如何 run the code for beginners
More detailed instructions on how to run the code for beginners |
2020-09-24 | 1 |
| #544 |
Code misbehavior(mild) : LinkedList.append(), according to code, 应该 添加 to tail, but actually append to head
Code misbehavior(mild) : LinkedList.append(), according to code, should add to tail, but actually append to head |
2020-09-22 | 1 |
| #543 |
Two way binding
Two way binding |
2020-09-20 | 1 |
| #498 |
Counting Sort optimization
Counting Sort optimization |
2020-06-03 | 1 |
| #460 |
应该 it be this right ?
should it be this right ? |
2020-02-07 | 1 |
| #434 |
BFS 图片 has a generic tree in it, but the algorithm only works with a binary trees
BFS image has a generic tree in it, but the algorithm only works with a binary trees |
2019-12-10 | 1 |