Skip to content

Commit a8a021a

Browse files
authored
Merge pull request #1 from trekhleb/master
Pull from trekhleb/javascript-algorithms
2 parents c6aa8ab + 924066b commit a8a021a

39 files changed

+2160
-124
lines changed

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: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@
66
This repository contains JavaScript based examples of many
77
popular algorithms and data structures.
88

9-
Each algorithm and data structure have its own separate README
10-
with related explanations and links for further reading and YouTube
11-
videos.
9+
Each algorithm and data structure has its own separate README
10+
with related explanations and links for further reading (including ones
11+
to YouTube videos).
1212

13-
_Read this in other languages:_ [简体中文](https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-CN.md)
13+
_Read this in other languages:_
14+
[简体中文](https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-CN.md),
15+
[繁體中文](https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-TW.md)
1416

1517
## Data Structures
1618

17-
Data structure is a particular way of organizing and storing data in a computer so that it can
19+
A data structure is a particular way of organizing and storing data in a computer so that it can
1820
be accessed and modified efficiently. More precisely, a data structure is a collection of data
1921
values, the relationships among them, and the functions or operations that can be applied to
2022
the data.
@@ -29,17 +31,14 @@ the data.
2931
* [Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree)
3032
* [Binary Search Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/binary-search-tree)
3133
* [AVL Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/avl-tree)
32-
* Red-Black Tree
33-
* Suffix Tree
34-
* Segment Tree or Interval Tree
35-
* Binary Indexed Tree or Fenwick Tree
34+
* [Red-Black Tree](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/red-black-tree)
3635
* [Graph](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/graph) (both directed and undirected)
3736
* [Disjoint Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/disjoint-set)
3837

3938
## Algorithms
4039

41-
Algorithm is an unambiguous specification of how to solve a class of problems. Algorithm is
42-
a set of rules that precisely defines a sequence of operations.
40+
An algorithm is an unambiguous specification of how to solve a class of problems. It is
41+
a set of rules that precisely define a sequence of operations.
4342

4443
### Algorithms by Topic
4544

@@ -52,7 +51,7 @@ a set of rules that precisely defines a sequence of operations.
5251
* [Integer Partition](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition)
5352
* **Sets**
5453
* [Cartesian Product](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/cartesian-product) - product of multiple sets
55-
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/power-set) - all subsets of the set
54+
* [Power Set](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/power-set) - all subsets of a set
5655
* [Permutations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/permutations) (with and without repetitions)
5756
* [Combinations](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/combinations) (with and without repetitions)
5857
* [Fisher–Yates Shuffle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/fisher-yates) - random permutation of a finite sequence
@@ -61,26 +60,29 @@ a set of rules that precisely defines a sequence of operations.
6160
* [Shortest Common Supersequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/shortest-common-supersequence) (SCS)
6261
* [Knapsack Problem](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/knapsack-problem) - "0/1" and "Unbound" ones
6362
* [Maximum Subarray](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/maximum-subarray) - "Brute Force" and "Dynamic Programming" (Kadane's) versions
64-
* **String**
63+
* **Strings**
6564
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
6665
* [Hamming Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/hamming-distance) - number of positions at which the symbols are different
6766
* [Knuth–Morris–Pratt Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/knuth-morris-pratt) - substring search
6867
* [Rabin Karp Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/rabin-karp) - substring search
6968
* [Longest Common Substring](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/longest-common-substring)
70-
* **Search**
69+
* **Searches**
70+
* [Linear Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/linear-search)
7171
* [Binary Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/search/binary-search)
7272
* **Sorting**
7373
* [Bubble Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/bubble-sort)
7474
* [Selection Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/selection-sort)
7575
* [Insertion Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/insertion-sort)
7676
* [Heap Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/heap-sort)
7777
* [Merge Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/merge-sort)
78-
* [Quicksort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/quick-sort)
78+
* [Quicksort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/quick-sort) - in-place and non-in-place implementations
7979
* [Shellsort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/shell-sort)
80-
* **Tree**
80+
* [Counting Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/counting-sort)
81+
* [Radix Sort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/radix-sort)
82+
* **Trees**
8183
* [Depth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search) (DFS)
8284
* [Breadth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/breadth-first-search) (BFS)
83-
* **Graph**
85+
* **Graphs**
8486
* [Depth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/depth-first-search) (DFS)
8587
* [Breadth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/breadth-first-search) (BFS)
8688
* [Dijkstra Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/dijkstra) - finding shortest path to all graph vertices
@@ -124,7 +126,7 @@ algorithm is an abstraction higher than a computer program.
124126
* [Quicksort](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sorting/quick-sort)
125127
* [Tree Depth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/tree/depth-first-search) (DFS)
126128
* [Graph Depth-First Search](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/depth-first-search) (DFS)
127-
* **Dynamic Programming** - build up to a solution using previously found sub-solutions
129+
* **Dynamic Programming** - build up a solution using previously found sub-solutions
128130
* [Fibonacci Number](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/fibonacci)
129131
* [Levenshtein Distance](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/string/levenshtein-distance) - minimum edit distance between two sequences
130132
* [Longest Common Subsequence](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/longest-common-subsequnce) (LCS)
@@ -135,9 +137,9 @@ algorithm is an abstraction higher than a computer program.
135137
* [Integer Partition](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/math/integer-partition)
136138
* [Maximum Subarray](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/sets/maximum-subarray)
137139
* [Bellman-Ford Algorithm](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/bellman-ford) - finding shortest path to all graph vertices
138-
* **Backtracking** - similarly to brute force try to generate all possible solutions but each time you generate a solution test
139-
if it satisfies all conditions, and only then continue generating subsequent solutions. Otherwise backtrack and go on a
140-
different path of finding solution
140+
* **Backtracking** - similarly to brute force, try to generate all possible solutions, but each time you generate a solution test
141+
if it satisfies all conditions, and only then continue generating subsequent solutions. Otherwise, backtrack, and go on a
142+
different path of finding a solution
141143
* [Hamiltonian Cycle](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/graph/hamiltonian-cycle) - Visit every vertex exactly once
142144
* [N-Queens Problem](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/uncategorized/n-queens)
143145
* [Knight's Tour](https://github.com/trekhleb/javascript-algorithms/tree/master/src/algorithms/uncategorized/knight-tour)
@@ -213,12 +215,14 @@ Below is the list of some of the most used Big O notations and their performance
213215

214216
### Array Sorting Algorithms Complexity
215217

216-
| Name | Best | Average | Worst | Memory | Stable |
217-
| --------------------- | :-------: | :-------: | :-----------: | :-------: | :-------: |
218-
| **Bubble sort** | n | n^2 | n^2 | 1 | Yes |
219-
| **Insertion sort** | n | n^2 | n^2 | 1 | Yes |
220-
| **Selection sort** | n^2 | n^2 | n^2 | 1 | No |
221-
| **Heap sort** | n log(n) | n log(n) | n log(n) | 1 | No |
222-
| **Merge sort** | n log(n) | n log(n) | n log(n) | n | Yes |
223-
| **Quick sort** | n log(n) | n log(n) | n^2 | log(n) | No |
224-
| **Shell sort** | n log(n) | depends on gap sequence | n (log(n))^2 | 1 | No |
218+
| Name | Best | Average | Worst | Memory | Stable | Comments |
219+
| --------------------- | :-------: | :-------: | :-----------: | :-------: | :-------: | :-------- |
220+
| **Bubble sort** | n | n^2 | n^2 | 1 | Yes | |
221+
| **Insertion sort** | n | n^2 | n^2 | 1 | Yes | |
222+
| **Selection sort** | n^2 | n^2 | n^2 | 1 | No | |
223+
| **Heap sort** | n log(n) | n log(n) | n log(n) | 1 | No | |
224+
| **Merge sort** | n log(n) | n log(n) | n log(n) | n | Yes | |
225+
| **Quick sort** | n log(n) | n log(n) | n^2 | log(n) | No | |
226+
| **Shell sort** | n log(n) | depends on gap sequence | n (log(n))^2 | 1 | No | |
227+
| **Counting sort** | n + r | n + r | n + r | n + r | Yes | r - biggest number in array |
228+
| **Radix sort** | n * k | n * k | n * k | n + k | Yes | k - length of longest key |

README.zh-CN.md

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77

88
每种算法和数据结构都有自己的 README 并提供相关说明以及进一步阅读和 YouTube 视频。
99

10-
_Read this in other languages:_ [_English_](https://github.com/trekhleb/javascript-algorithms/)
10+
_Read this in other languages:_
11+
[_English_](https://github.com/trekhleb/javascript-algorithms/),
12+
[繁體中文](https://github.com/trekhleb/javascript-algorithms/blob/master/README.zh-TW.md)
1113

1214
## 数据结构
1315

@@ -23,10 +25,7 @@ _Read this in other languages:_ [_English_](https://github.com/trekhleb/javascri
2325
* [](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree)
2426
* [二分查找](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/binary-search-tree)
2527
* [AVL 树](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/avl-tree)
26-
* 红黑树
27-
* 后缀树
28-
* 线段树 或 间隔树
29-
* 二叉索引树
28+
* [红黑树](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/tree/red-black-tree)
3029
* [](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/graph) (有向图与无向图)
3130
* [并查集](https://github.com/trekhleb/javascript-algorithms/tree/master/src/data-structures/disjoint-set)
3231

0 commit comments

Comments
 (0)