Skip to content

Commit 2f59013

Browse files
authored
Merge pull request #1 from trekhleb/master
Update fork
2 parents c6aa8ab + f08fc37 commit 2f59013

File tree

321 files changed

+16414
-2680
lines changed

Some content is hidden

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

321 files changed

+16414
-2680
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

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ dist: trusty
33
language: node_js
44
node_js:
55
- node
6+
install:
7+
- npm install -g codecov
8+
- npm install
69
script:
710
- npm run ci
8-
- npm run codecov
11+
- codecov
912
notifications:
1013
email: false

CONTRIBUTING.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,25 @@
11
## Contributing
22

3+
**General Rules**
4+
35
- As much as possible, try to follow the existing format of markdown and code.
46
- Don't forget to run `npm run lint` and `npm test` before submitting pull requests.
57
- Make sure that **100%** of your code is covered by tests.
8+
9+
**Contributing New Translation**
10+
11+
- Create new `README.xx-XX.md` file with translation alongside with
12+
main `README.md` file where `xx-XX` is [locale and country/region codes](http://www.lingoes.net/en/translator/langcode.htm).
13+
For example `en-US`, `zh-CN`, `zh-TW`, `ko-KR` etc.
14+
- You may also translate all other sub-folders by creating
15+
related `README.xx-XX.md` files in each of them.
16+
17+
**Contributing New Algorithms**
18+
19+
- Make your pull requests to be **specific** and **focused**. Instead of
20+
contributing "several sorting algorithms" all at once contribute them all
21+
one by one separately (i.e. one pull request for "Quick Sort", another one
22+
for "Heap Sort" and so on).
23+
- Provide **README.md** for each of the algorithms **with explanations** of
24+
the algorithm and **with links** to further readings.
25+
- Describe what you do in code using **comments**.

README.es-ES.md

Lines changed: 294 additions & 0 deletions
Large diffs are not rendered by default.

README.fr-FR.md

Lines changed: 282 additions & 0 deletions
Large diffs are not rendered by default.

README.ja-JP.md

Lines changed: 291 additions & 0 deletions
Large diffs are not rendered by default.

README.ko-KR.md

Lines changed: 272 additions & 0 deletions
Large diffs are not rendered by default.

README.md

Lines changed: 212 additions & 134 deletions
Large diffs are not rendered by default.

README.pl-PL.md

Lines changed: 284 additions & 0 deletions
Large diffs are not rendered by default.

README.pt-BR.md

Lines changed: 285 additions & 0 deletions
Large diffs are not rendered by default.

README.zh-CN.md

Lines changed: 202 additions & 131 deletions
Large diffs are not rendered by default.

README.zh-TW.md

Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
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+
每個演算法和資料結構都有其個別的文件,內有相關的解釋以及更多相關的文章或Youtube影片連結。
8+
9+
_Read this in other languages:_
10+
[_English_](https://github.com/trekhleb/javascript-algorithms/),
11+
[_简体中文_](README.zh-CN.md),
12+
[_한국어_](README.ko-KR.md),
13+
[_日本語_](README.ja-JP.md),
14+
[_Polski_](README.pl-PL.md),
15+
[_Français_](README.fr-FR.md),
16+
[_Español_](README.es-ES.md),
17+
[_Português_](README.pt-BR.md)
18+
19+
## 資料結構
20+
21+
資料結構是一個電腦用來組織和排序資料的特定方式,透過這樣的方式資料可以有效率地被讀取以及修改。更精確地說,一個資料結構是一個資料值的集合、彼此間的關係,函數或者運作可以應用於資料上。
22+
23+
* [鏈結串列](src/data-structures/linked-list)
24+
* [貯列](src/data-structures/queue)
25+
* [堆疊](src/data-structures/stack)
26+
* [雜湊表](src/data-structures/hash-table)
27+
* [](src/data-structures/heap)
28+
* [優先貯列](src/data-structures/priority-queue)
29+
* [字典樹](src/data-structures/trie)
30+
* [](src/data-structures/tree)
31+
* [二元搜尋樹](src/data-structures/tree/binary-search-tree)
32+
* [AVL樹](src/data-structures/tree/avl-tree)
33+
* [紅黑樹](src/data-structures/tree/red-black-tree)
34+
* [](src/data-structures/graph) (有向跟無向皆包含)
35+
* [互斥集](src/data-structures/disjoint-set)
36+
37+
## 演算法
38+
39+
演算法是一個如何解決一類問題的非模糊規格。演算法是一個具有精確地定義了一系列運作的規則的集合
40+
41+
### 演算法議題分類
42+
43+
* **數學類**
44+
* [階層](src/algorithms/math/factorial)
45+
* [費伯納西數列](src/algorithms/math/fibonacci)
46+
* [Primality Test](src/algorithms/math/primality-test) (排除法)
47+
* [歐幾里得算法](src/algorithms/math/euclidean-algorithm) - 計算最大公因數 (GCD)
48+
* [最小公倍數](src/algorithms/math/least-common-multiple) (LCM)
49+
* [整數拆分](src/algorithms/math/integer-partition)
50+
* **集合**
51+
* [笛卡爾積](src/algorithms/sets/cartesian-product) - 多個集合的乘積
52+
* [冪集合](src/algorithms/sets/power-set) - 所有集合的子集合
53+
* [排列](src/algorithms/sets/permutations) (有/無重複)
54+
* [组合](src/algorithms/sets/combinations) (有/無重複)
55+
* [洗牌算法](src/algorithms/sets/fisher-yates) - 隨機置換一有限序列
56+
* [最長共同子序列](src/algorithms/sets/longest-common-subsequence) (LCS)
57+
* [最長遞增子序列](src/algorithms/sets/longest-increasing-subsequence)
58+
* [Shortest Common Supersequence](src/algorithms/sets/shortest-common-supersequence) (SCS)
59+
* [背包問題](src/algorithms/sets/knapsack-problem) - "0/1" and "Unbound" ones
60+
* [最大子序列問題](src/algorithms/sets/maximum-subarray) - 暴力法以及動態編程的(Kadane's)版本
61+
* **字串**
62+
* [萊文斯坦距離](src/algorithms/string/levenshtein-distance) - 兩序列間的最小編輯距離
63+
* [漢明距離](src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
64+
* [KMP 演算法](src/algorithms/string/knuth-morris-pratt) - 子字串搜尋
65+
* [Rabin Karp 演算法](src/algorithms/string/rabin-karp) - 子字串搜尋
66+
* [最長共通子序列](src/algorithms/string/longest-common-substring)
67+
* **搜尋**
68+
* [二元搜尋](src/algorithms/search/binary-search)
69+
* **排序**
70+
* [氣泡排序](src/algorithms/sorting/bubble-sort)
71+
* [選擇排序](src/algorithms/sorting/selection-sort)
72+
* [插入排序](src/algorithms/sorting/insertion-sort)
73+
* [堆排序](src/algorithms/sorting/heap-sort)
74+
* [合併排序](src/algorithms/sorting/merge-sort)
75+
* [快速排序](src/algorithms/sorting/quick-sort)
76+
* [希爾排序](src/algorithms/sorting/shell-sort)
77+
* ****
78+
* [深度優先搜尋](src/algorithms/tree/depth-first-search) (DFS)
79+
* [廣度優先搜尋](src/algorithms/tree/breadth-first-search) (BFS)
80+
* ****
81+
* [深度優先搜尋](src/algorithms/graph/depth-first-search) (DFS)
82+
* [廣度優先搜尋](src/algorithms/graph/breadth-first-search) (BFS)
83+
* [Dijkstra 演算法](src/algorithms/graph/dijkstra) - 找到所有圖頂點的最短路徑
84+
* [Bellman-Ford 演算法](src/algorithms/graph/bellman-ford) - 找到所有圖頂點的最短路徑
85+
* [Detect Cycle](src/algorithms/graph/detect-cycle) - for both directed and undirected graphs (DFS and Disjoint Set based versions)
86+
* [Prim’s 演算法](src/algorithms/graph/prim) - finding Minimum Spanning Tree (MST) for weighted undirected graph
87+
* [Kruskal’s 演算法](src/algorithms/graph/kruskal) - finding Minimum Spanning Tree (MST) for weighted undirected graph
88+
* [拓撲排序](src/algorithms/graph/topological-sorting) - DFS method
89+
* [關節點](src/algorithms/graph/articulation-points) - Tarjan's algorithm (DFS based)
90+
* [](src/algorithms/graph/bridges) - DFS based algorithm
91+
* [尤拉路徑及尤拉環](src/algorithms/graph/eulerian-path) - Fleury's algorithm - Visit every edge exactly once
92+
* [漢彌爾頓環](src/algorithms/graph/hamiltonian-cycle) - Visit every vertex exactly once
93+
* [強連通組件](src/algorithms/graph/strongly-connected-components) - Kosaraju's algorithm
94+
* [旅行推銷員問題](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city
95+
* [Floyd-Warshall algorithm](src/algorithms/graph/floyd-warshall) - 一次循环可以找出所有頂點之间的最短路徑
96+
* **未分類**
97+
* [河內塔](src/algorithms/uncategorized/hanoi-tower)
98+
* [N-皇后問題](src/algorithms/uncategorized/n-queens)
99+
* [騎士走棋盤](src/algorithms/uncategorized/knight-tour)
100+
101+
### 演算法範型
102+
103+
演算法的範型是一個泛用方法或設計一類底層演算法的方式。它是一個比演算法的概念更高階的抽象化,就像是演算法是比電腦程式更高階的抽象化。
104+
105+
* **暴力法** - 尋遍所有的可能解然後選取最好的解
106+
* [最大子序列](src/algorithms/sets/maximum-subarray)
107+
* [旅行推銷員問題](src/algorithms/graph/travelling-salesman) - shortest possible route that visits each city and returns to the origin city
108+
* **貪婪法** - choose the best option at the current time, without any consideration for the future
109+
* [未定背包問題](src/algorithms/sets/knapsack-problem)
110+
* [Dijkstra 演算法](src/algorithms/graph/dijkstra) - 找到所有圖頂點的最短路徑
111+
* [Prim’s 演算法](src/algorithms/graph/prim) - finding Minimum Spanning Tree (MST) for weighted undirected graph
112+
* [Kruskal’s 演算法](src/algorithms/graph/kruskal) - finding Minimum Spanning Tree (MST) for weighted undirected graph
113+
* **分治法** - divide the problem into smaller parts and then solve those parts
114+
* [二元搜尋](src/algorithms/search/binary-search)
115+
* [河內塔](src/algorithms/uncategorized/hanoi-tower)
116+
* [歐幾里得演算法](src/algorithms/math/euclidean-algorithm) - calculate the Greatest Common Divisor (GCD)
117+
* [排列](src/algorithms/sets/permutations) (有/無重複)
118+
* [组合](src/algorithms/sets/combinations) (有/無重複)
119+
* [合併排序](src/algorithms/sorting/merge-sort)
120+
* [快速排序](src/algorithms/sorting/quick-sort)
121+
* [樹深度優先搜尋](src/algorithms/tree/depth-first-search) (DFS)
122+
* [圖深度優先搜尋](src/algorithms/graph/depth-first-search) (DFS)
123+
* **動態編程** - build up to a solution using previously found sub-solutions
124+
* [費伯納西數列](src/algorithms/math/fibonacci)
125+
* [萊溫斯坦距離](src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
126+
* [最長共同子序列](src/algorithms/sets/longest-common-subsequence) (LCS)
127+
* [最長共同子字串](src/algorithms/string/longest-common-substring)
128+
* [最長遞增子序列](src/algorithms/sets/longest-increasing-subsequence)
129+
* [最短共同子序列](src/algorithms/sets/shortest-common-supersequence)
130+
* [0/1背包問題](src/algorithms/sets/knapsack-problem)
131+
* [整數拆分](src/algorithms/math/integer-partition)
132+
* [最大子序列](src/algorithms/sets/maximum-subarray)
133+
* [Bellman-Ford 演算法](src/algorithms/graph/bellman-ford) - finding shortest path to all graph vertices
134+
* **回溯法** - 用類似暴力法來嘗試產生所有可能解,但每次只在能滿足所有測試條件,且只有繼續產生子序列方案來產生的解決方案。否則回溯並尋找不同路徑的解決方案。
135+
* [漢彌爾頓迴路](src/algorithms/graph/hamiltonian-cycle) - Visit every vertex exactly once
136+
* [N-皇后問題](src/algorithms/uncategorized/n-queens)
137+
* [騎士走棋盤](src/algorithms/uncategorized/knight-tour)
138+
* **Branch & Bound**
139+
140+
## 如何使用本知識庫
141+
142+
**安裝所有必須套件**
143+
144+
```
145+
npm install
146+
```
147+
148+
**執行所有測試**
149+
```
150+
npm test
151+
```
152+
153+
**以名稱執行該測試**
154+
```
155+
npm test -- 'LinkedList'
156+
```
157+
**練習場**
158+
159+
你可以透過在`./src/playground/playground.js`裡面的檔案練習資料結構以及演算法,並且撰寫在`./src/playground/__test__/playground.test.js`裡面的測試程式。
160+
161+
接著直接執行下列的指令來測試你練習的 code 是否如預期運作:
162+
163+
```
164+
npm test -- 'playground'
165+
```
166+
167+
## 有用的資訊
168+
169+
### 參考
170+
171+
[▶ Data Structures and Algorithms on YouTube](https://www.youtube.com/playlist?list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8)
172+
173+
### 大 O 標記
174+
175+
特別用大 O 標記演算法增長度的排序。
176+
177+
![Big O 表](./assets/big-o-graph.png)
178+
179+
資料來源: [Big O Cheat Sheet](http://bigocheatsheet.com/).
180+
181+
下列列出幾個常用的 Big O 標記以及其不同大小資料量輸入後的運算效能比較。
182+
183+
| Big O 標記 | 10個資料量需花費的時間 | 100個資料量需花費的時間 | 1000個資料量需花費的時間 |
184+
| -------------- | ---------------------------- | ----------------------------- | ------------------------------- |
185+
| **O(1)** | 1 | 1 | 1 |
186+
| **O(log N)** | 3 | 6 | 9 |
187+
| **O(N)** | 10 | 100 | 1000 |
188+
| **O(N log N)** | 30 | 600 | 9000 |
189+
| **O(N^2)** | 100 | 10000 | 1000000 |
190+
| **O(2^N)** | 1024 | 1.26e+29 | 1.07e+301 |
191+
| **O(N!)** | 3628800 | 9.3e+157 | 4.02e+2567 |
192+
193+
### 資料結構運作複雜度
194+
195+
| 資料結構 | 存取 | 搜尋 | 插入 | 刪除 |
196+
| ----------------------- | :-------: | :-------: | :-------: | :-------: |
197+
| **陣列** | 1 | n | n | n |
198+
| **堆疊** | n | n | 1 | 1 |
199+
| **貯列** | n | n | 1 | 1 |
200+
| **鏈結串列** | n | n | 1 | 1 |
201+
| **雜湊表** | - | n | n | n |
202+
| **二元搜尋樹** | n | n | n | n |
203+
| **B-Tree** | log(n) | log(n) | log(n) | log(n) |
204+
| **紅黑樹** | log(n) | log(n) | log(n) | log(n) |
205+
| **AVL Tree** | log(n) | log(n) | log(n) | log(n) |
206+
207+
### 陣列排序演算法複雜度
208+
209+
| 名稱 | 最佳 | 平均 | 最差 | 記憶體 | 穩定 |
210+
| ---------------------- | :-------: | :-------: | :-----------: | :-------: | :-------: |
211+
| **氣派排序** | n | n^2 | n^2 | 1 | Yes |
212+
| **插入排序** | n | n^2 | n^2 | 1 | Yes |
213+
| **選擇排序** | n^2 | n^2 | n^2 | 1 | No |
214+
| **Heap 排序** | n log(n) | n log(n) | n log(n) | 1 | No |
215+
| **合併排序** | n log(n) | n log(n) | n log(n) | n | Yes |
216+
| **快速排序** | n log(n) | n log(n) | n^2 | log(n) | No |
217+
| **希爾排序** | n log(n) | 由gap sequence決定 | n (log(n))^2 | 1 | No |

assets/big-o-graph.png

-28.6 KB
Loading

jest.config.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,28 @@
11
module.exports = {
2+
// The bail config option can be used here to have Jest stop running tests after
3+
// the first failure.
4+
bail: false,
5+
6+
// Indicates whether each individual test should be reported during the run.
27
verbose: false,
8+
9+
// Indicates whether the coverage information should be collected while executing the test
310
collectCoverage: false,
11+
12+
// The directory where Jest should output its coverage files.
413
coverageDirectory: './coverage/',
14+
15+
// If the test path matches any of the patterns, it will be skipped.
16+
testPathIgnorePatterns: ['<rootDir>/node_modules/'],
17+
18+
// If the file path matches any of the patterns, coverage information will be skipped.
19+
coveragePathIgnorePatterns: ['<rootDir>/node_modules/'],
20+
21+
// The pattern Jest uses to detect test files.
22+
testRegex: '(/__tests__/.*|(\\.|/)(test|spec))\\.jsx?$',
23+
24+
// This option sets the URL for the jsdom environment.
25+
// It is reflected in properties such as location.href.
26+
// @see: https://github.com/facebook/jest/issues/6769
27+
testURL: 'http://localhost/',
528
};

0 commit comments

Comments
 (0)