Skip to content

Commit ea99e8f

Browse files
authored
Merge pull request #1 from trekhleb/master
Updates
2 parents 3e0ac74 + 3e8540b commit ea99e8f

File tree

175 files changed

+9351
-1576
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

175 files changed

+9351
-1576
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
node_modules
22
.idea
33
coverage
4+
.vscode
5+
.DS_Store

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@
33
- As much as possible, try to follow the existing format of markdown and code.
44
- Don't forget to run `npm run lint` and `npm test` before submitting pull requests.
55
- Make sure that **100%** of your code is covered by tests.
6+
- If you're adding **new** algorithms or data structures please provide **README.md** for each of them **with explanations** of the algorithm and **with links** to further readings.

README.md

Lines changed: 185 additions & 141 deletions
Large diffs are not rendered by default.

README.zh-CN.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
# JavaScript 算法与数据结构
2+
3+
[![build status](https://travis-ci.org/trekhleb/javascript-algorithms.svg?branch=master)](https://travis-ci.org/trekhleb/javascript-algorithms)
4+
[![codecov](https://codecov.io/gh/trekhleb/javascript-algorithms/branch/master/graph/badge.svg)](https://codecov.io/gh/trekhleb/javascript-algorithms)
5+
6+
本仓库包含了多种基于 JavaScript 的算法与数据结构。
7+
8+
每种算法和数据结构都有自己的 README 并提供相关说明以及进一步阅读和 YouTube 视频。
9+
10+
_Read this in other languages:_
11+
[_English_](https://github.com/trekhleb/javascript-algorithms/),
12+
[繁體中文](README.zh-TW.md)
13+
14+
## 数据结构
15+
16+
数据结构是在计算机中组织和存储数据的一种特殊方式,它可以高效地访问和修改数据。更确切地说,数据结构是数据值的集合,它们之间的关系、函数或操作可以应用于数据。
17+
18+
* [链表](src/data-structures/linked-list)
19+
* [队列](src/data-structures/queue)
20+
* [](src/data-structures/stack)
21+
* [哈希表](src/data-structures/hash-table)
22+
* [](src/data-structures/heap)
23+
* [优先队列](src/data-structures/priority-queue)
24+
* [字典树](src/data-structures/trie)
25+
* [](src/data-structures/tree)
26+
* [二分查找](src/data-structures/tree/binary-search-tree)
27+
* [AVL 树](src/data-structures/tree/avl-tree)
28+
* [红黑树](src/data-structures/tree/red-black-tree)
29+
* [](src/data-structures/graph) (有向图与无向图)
30+
* [并查集](src/data-structures/disjoint-set)
31+
32+
## 算法
33+
34+
算法是如何解决一类问题的明确规范。 算法是一组精确定义操作序列的规则。
35+
36+
### 算法主题
37+
38+
* **数学**
39+
* [阶乘](src/algorithms/math/factorial)
40+
* [斐波那契数](src/algorithms/math/fibonacci)
41+
* [素数检测](src/algorithms/math/primality-test) (排除法)
42+
* [欧几里得算法](src/algorithms/math/euclidean-algorithm) - 计算最大公约数(GCD)
43+
* [最小公倍数](src/algorithms/math/least-common-multiple) (LCM)
44+
* [整数拆分](src/algorithms/math/integer-partition)
45+
* **集合**
46+
* [笛卡尔积](src/algorithms/sets/cartesian-product) - 多集合结果
47+
* [幂集](src/algorithms/sets/power-set) - 该集合的所有子集
48+
* [排列](src/algorithms/sets/permutations) (有/无重复)
49+
* [组合](src/algorithms/sets/combinations) (有/无重复)
50+
* [洗牌算法](src/algorithms/sets/fisher-yates) - 随机置换有限序列
51+
* [最长公共子序列](src/algorithms/sets/longest-common-subsequence) (LCS)
52+
* [最长递增子序列](src/algorithms/sets/longest-increasing-subsequence)
53+
* [Shortest Common Supersequence](src/algorithms/sets/shortest-common-supersequence) (SCS)
54+
* [背包问题](src/algorithms/sets/knapsack-problem) - "0/1" and "Unbound" ones
55+
* [最大子数列问题](src/algorithms/sets/maximum-subarray) - BF算法 与 动态编程
56+
* **字符串**
57+
* [莱温斯坦距离](src/algorithms/string/levenshtein-distance) - 两个序列之间的最小编辑距离
58+
* [汉明距离](src/algorithms/string/hamming-distance) - 符号不同的位置数
59+
* [克努斯-莫里斯-普拉特算法](src/algorithms/string/knuth-morris-pratt) - 子串搜索
60+
* [字符串快速查找](src/algorithms/string/rabin-karp) - 子串搜索
61+
* [最长公共子串](src/algorithms/string/longest-common-substring)
62+
* **搜索**
63+
* [二分查找](src/algorithms/search/binary-search)
64+
* **排序**
65+
* [冒泡排序](src/algorithms/sorting/bubble-sort)
66+
* [选择排序](src/algorithms/sorting/selection-sort)
67+
* [插入排序](src/algorithms/sorting/insertion-sort)
68+
* [堆排序](src/algorithms/sorting/heap-sort)
69+
* [归并排序](src/algorithms/sorting/merge-sort)
70+
* [快速排序](src/algorithms/sorting/quick-sort)
71+
* [希尔排序](src/algorithms/sorting/shell-sort)
72+
* ****
73+
* [深度优先搜索](src/algorithms/tree/depth-first-search) (DFS)
74+
* [广度优先搜索](src/algorithms/tree/breadth-first-search) (BFS)
75+
* ****
76+
* [深度优先搜索](src/algorithms/graph/depth-first-search) (DFS)
77+
* [广度优先搜索](src/algorithms/graph/breadth-first-search) (BFS)
78+
* [戴克斯特拉算法m](src/algorithms/graph/dijkstra) - 找到所有图顶点的最短路径
79+
* [贝尔曼-福特算法](src/algorithms/graph/bellman-ford) - 找到所有图顶点的最短路径
80+
* [判圈算法](src/algorithms/graph/detect-cycle) - 对于有向图和无向图(基于DFS和不相交集的版本)
81+
* [普林演算法](src/algorithms/graph/prim) - 寻找加权无向图的最小生成树(MST)
82+
* [克鲁斯克尔演算法](src/algorithms/graph/kruskal) - 寻找加权无向图的最小生成树(MST)
83+
* [拓撲排序](src/algorithms/graph/topological-sorting) - DFS 方法
84+
* [关节点](src/algorithms/graph/articulation-points) - Tarjan算法(基于DFS)
85+
* [](src/algorithms/graph/bridges) - 基于DFS的算法
86+
* [欧拉路径与一笔画问题](src/algorithms/graph/eulerian-path) - Fleury的算法 - 一次访问每个边缘
87+
* [哈密顿图](src/algorithms/graph/hamiltonian-cycle) - 恰好访问每个顶点一次
88+
* [强连通分量](src/algorithms/graph/strongly-connected-components) - Kosaraju算法
89+
* [旅行推销员问题](src/algorithms/graph/travelling-salesman) - 尽可能以最短的路线访问每个城市并返回原始城市
90+
* **未分类**
91+
* [汉诺塔](src/algorithms/uncategorized/hanoi-tower)
92+
* [八皇后问题](src/algorithms/uncategorized/n-queens)
93+
* [骑士巡逻](src/algorithms/uncategorized/knight-tour)
94+
95+
### 算法范式
96+
97+
算法范式是基于类的设计的通用方法或方法的算法。 这是一个比算法概念更高的抽象,就像一个
98+
算法是比计算机程序更高的抽象。
99+
100+
* **BF算法** - 查找所有可能性并选择最佳解决方案
101+
* [最大子数列](src/algorithms/sets/maximum-subarray)
102+
* [旅行推销员问题](src/algorithms/graph/travelling-salesman) - 尽可能以最短的路线访问每个城市并返回原始城市
103+
104+
* **贪心法** - 在当前选择最佳选项,不考虑以后情况
105+
* [背包问题](src/algorithms/sets/knapsack-problem)
106+
* [戴克斯特拉算法](src/algorithms/graph/dijkstra) - 找到所有图顶点的最短路径
107+
* [普里姆算法](src/algorithms/graph/prim) - 寻找加权无向图的最小生成树(MST)
108+
* [克鲁斯卡尔算法](src/algorithms/graph/kruskal) - 寻找加权无向图的最小生成树(MST)
109+
* **分治法** - 将问题分成较小的部分,然后解决这些部分
110+
* [二分查找](src/algorithms/search/binary-search)
111+
* [汉诺塔](src/algorithms/uncategorized/hanoi-tower)
112+
* [欧几里得算法](src/algorithms/math/euclidean-algorithm) - 计算最大公约数(GCD)
113+
* [排列](src/algorithms/sets/permutations) (有/无重复)
114+
* [组合](src/algorithms/sets/combinations) (有/无重复)
115+
* [归并排序](src/algorithms/sorting/merge-sort)
116+
* [Quicksort](src/algorithms/sorting/quick-sort)
117+
* [树深度优先搜索](src/algorithms/tree/depth-first-search) (DFS)
118+
* [图深度优先搜索](src/algorithms/graph/depth-first-search) (DFS)
119+
* **动态编程** - 使用以前找到的子解决方案构建解决方案
120+
* [斐波那契数](src/algorithms/math/fibonacci)
121+
* [莱温斯坦距离](src/algorithms/string/levenshtein-distance) - 两个序列之间的最小编辑距离
122+
* [最长公共子序列](src/algorithms/sets/longest-common-subsequence) (LCS)
123+
* [最长公共子串](src/algorithms/string/longest-common-substring)
124+
* [最长递增子序列](src/algorithms/sets/longest-increasing-subsequence)
125+
* [最短公共子序列](src/algorithms/sets/shortest-common-supersequence)
126+
* [0-1背包问题](src/algorithms/sets/knapsack-problem)
127+
* [整数拆分](src/algorithms/math/integer-partition)
128+
* [最大子数列](src/algorithms/sets/maximum-subarray)
129+
* [贝尔曼-福特算法](src/algorithms/graph/bellman-ford) - 找到所有图顶点的最短路径
130+
* **回溯法** - 类似于 BF算法 试图产生所有可能的解决方案,但每次生成解决方案测试如果它满足所有条件,那么只有继续生成后续解决方案。 否则回溯并继续寻找不同路径的解决方案。
131+
* [哈密顿图](src/algorithms/graph/hamiltonian-cycle) - 恰好访问每个顶点一次
132+
* [八皇后问题](src/algorithms/uncategorized/n-queens)
133+
* [骑士巡逻](src/algorithms/uncategorized/knight-tour)
134+
* **B & B**
135+
136+
## 如何使用本仓库
137+
138+
**安装依赖**
139+
```
140+
npm install
141+
```
142+
143+
**执行测试**
144+
```
145+
npm test
146+
```
147+
148+
**按照名称执行测试**
149+
```
150+
npm test -- 'LinkedList'
151+
```
152+
153+
**Playground**
154+
155+
你可以在`./src/playground/playground.js`文件中操作数据结构与算法,并在`./src/playground/__test__/playground.test.js`中编写测试。
156+
157+
然后,只需运行以下命令来测试你的 Playground 是否按无误:
158+
159+
```
160+
npm test -- 'playground'
161+
```
162+
163+
## 有用的信息
164+
165+
### 引用
166+
167+
[▶ YouTube](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
168+
169+
### 大O符号
170+
171+
大O符号中指定的算法的增长顺序。
172+
173+
![Big O graphs](./assets/big-o-graph.png)
174+
175+
源: [Big O Cheat Sheet](http://bigocheatsheet.com/).
176+
177+
以下是一些最常用的 大O标记法 列表以及它们与不同大小输入数据的性能比较。
178+
179+
| 大O标记法 | 计算10个元素 | 计算100个元素 | 计算1000个元素 |
180+
| -------------- | ---------------------------- | ----------------------------- | ------------------------------- |
181+
| **O(1)** | 1 | 1 | 1 |
182+
| **O(log N)** | 3 | 6 | 9 |
183+
| **O(N)** | 10 | 100 | 1000 |
184+
| **O(N log N)** | 30 | 600 | 9000 |
185+
| **O(N^2)** | 100 | 10000 | 1000000 |
186+
| **O(2^N)** | 1024 | 1.26e+29 | 1.07e+301 |
187+
| **O(N!)** | 3628800 | 9.3e+157 | 4.02e+2567 |
188+
189+
### 数据结构操作的复杂性
190+
191+
| 数据结构 | 连接 | 查找 | 插入 | 删除 |
192+
| ----------------------- | :-------: | :-------: | :-------: | :-------: |
193+
| **数组** | 1 | n | n | n |
194+
| **** | n | n | 1 | 1 |
195+
| **队列** | n | n | 1 | 1 |
196+
| **链表** | n | n | 1 | 1 |
197+
| **哈希表** | - | n | n | n |
198+
| **二分查找树** | n | n | n | n |
199+
| **B树** | log(n) | log(n) | log(n) | log(n) |
200+
| **红黑树** | log(n) | log(n) | log(n) | log(n) |
201+
| **AVL树** | log(n) | log(n) | log(n) | log(n) |
202+
203+
### 数组排序算法的复杂性
204+
205+
| 名称 | 最优 | 平均 | 最坏 | 内存 | 稳定 |
206+
| --------------------- | :-------: | :-------: | :-----------: | :-------: | :-------: |
207+
| **冒泡排序** | n | n^2 | n^2 | 1 | Yes |
208+
| **插入排序** | n | n^2 | n^2 | 1 | Yes |
209+
| **选择排序** | n^2 | n^2 | n^2 | 1 | No |
210+
| **堆排序** | n log(n) | n log(n) | n log(n) | 1 | No |
211+
| **归并排序** | n log(n) | n log(n) | n log(n) | n | Yes |
212+
| **快速排序** | n log(n) | n log(n) | n^2 | log(n) | No |
213+
| **希尔排序** | n log(n) | 取决于差距序列 | n (log(n))^2 | 1 | No |

0 commit comments

Comments
 (0)