Skip to content

Commit 6e9c16c

Browse files
committed
文档更新
1 parent 1800334 commit 6e9c16c

File tree

17 files changed

+148
-124
lines changed

17 files changed

+148
-124
lines changed

README.zh-CN.md

Lines changed: 7 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,5 @@
11
# JavaScript 算法与数据结构
22

3-
## TypeScript 版本
4-
5-
参考 https://github.com/loiane/javascript-datastructures-algorithms
6-
73
[![CI](https://github.com/trekhleb/javascript-algorithms/workflows/CI/badge.svg)](https://github.com/trekhleb/javascript-algorithms/actions?query=workflow%3ACI+branch%3Amaster)
84
[![codecov](https://codecov.io/gh/trekhleb/javascript-algorithms/branch/master/graph/badge.svg)](https://codecov.io/gh/trekhleb/javascript-algorithms)
95

@@ -288,7 +284,6 @@ npm test -- 'playground'
288284

289285
### 数组排序算法的复杂性
290286

291-
<<<<<<< HEAD
292287
| 名称 | 最优 | 平均 | 最坏 | 内存 | 稳定 | 备注 |
293288
| ------------ | :------: | :------------: | :----------: | :----: | :--: | ---------------------------------------------- |
294289
| **冒泡排序** | n | n^2 | n^2 | 1 | Yes | |
@@ -302,29 +297,16 @@ npm test -- 'playground'
302297
| **基数排序** | n \* k | n \* k | n \* k | n + k | Yes | k - 最长 key 的升序 |
303298

304299
> ℹ️ A few more [projects](https://trekhleb.dev/projects/) and [articles](https://trekhleb.dev/blog/) about JavaScript and algorithms on [trekhleb.dev](https://trekhleb.dev)
305-
=======
306-
| 名称 | 最优 | 平均 | 最坏 | 内存 | 稳定 | 备注 |
307-
| --------------------- | :-------: | :-------: | :-----------: | :-------: | :-------: | --------------------- |
308-
| **冒泡排序** | n | n^2 | n^2 | 1 | Yes | |
309-
| **插入排序** | n | n^2 | n^2 | 1 | Yes | |
310-
| **选择排序** | n^2 | n^2 | n^2 | 1 | No | |
311-
| **堆排序** | n log(n) | n log(n) | n log(n) | 1 | No | |
312-
| **归并排序** | n log(n) | n log(n) | n log(n) | n | Yes | |
313-
| **快速排序** | n log(n) | n log(n) | n^2 | log(n) | No | 在 in-place 版本下,内存复杂度通常是 O(log(n)) |
314-
| **希尔排序** | n log(n) | 取决于差距序列 | n (log(n))^2 | 1 | No | |
315-
| **计数排序** | n + r | n + r | n + r | n + r | Yes | r - 数组里最大的数 |
316-
| **基数排序** | n * k | n * k | n * k | n + k | Yes | k - 最长 key 的升序 |
317300
318301
## 扩展学习
319302

320-
* TypeScript 版本
321-
* 算法可视化
303+
- [TypeScript 版本](https://github.com/loiane/javascript-datastructures-algorithms)
304+
- [算法可视化](https://visualgo.net/zh)
322305

323306
参考资料
324307

325-
* https://github.com/loiane/javascript-datastructures-algorithms
326-
* https://visualgo.net/zh
327-
* https://coolshell.cn/articles/4671.html
328-
* https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
329-
* https://www.cs.usfca.edu/~galles/visualization/source.html
330-
>>>>>>> 5b541e4 (更新文档)
308+
- https://github.com/loiane/javascript-datastructures-algorithms
309+
- https://visualgo.net/zh
310+
- https://coolshell.cn/articles/4671.html
311+
- https://www.cs.usfca.edu/~galles/visualization/Algorithms.html
312+
- https://www.cs.usfca.edu/~galles/visualization/source.html

src/algorithms/math/bits/README.zh-CN.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ _Read this in other languages:_
44
[français](README.fr-FR.md),
55
[english](README.md)
66

7+
### Bit 操控
8+
9+
- set
10+
- get
11+
- update
12+
- clear
13+
-
14+
-
15+
- 变负
16+
717
#### Get Bit
818

919
该方法向右移动目标位到最右边,即位数组的第0个位置上。然后在该数上与形如 `0001`的二进制形式的数进行`AND`操作。这会清理掉除了目标位的所有其它位的数据。如果目标位是1,那么结果就是`1`,反之,结果是`0`;
@@ -226,7 +236,7 @@ B = 6: 110
226236
└──────┴────┴────┴─────────┴──────────┴─────────┴───────────┴───────────┘
227237
```
228238

229-
> 查看[fullAdder.js](fullAdder.js)了解更多细节。
239+
> 查看[fullAdder.js](fullAdder.js)了解更多细节。
230240
> 查看[Full Adder on YouTube](https://www.youtube.com/watch?v=wvJc9CZcvBc&list=PLLXdhg_r2hKA7DPDsunoDZ-Z769jWn4R8).
231241
232242
## References

src/algorithms/math/bits/bitLength.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Return the number of bits used in the binary representation of the number.
3+
* or number.toString(2).length
34
*
45
* @param {number} number
56
* @return {number}

src/algorithms/math/complex-number/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Complex Number
22

3+
复数 - 复数及其基本运算
4+
35
_Read this in other languages:_
46
[français](README.fr-FR.md).
57

src/algorithms/math/euclidean-algorithm/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Euclidean algorithm
22

3+
欧几里得算法 - 计算最大公约数 (GCD)
4+
35
_Read this in other languages:_
46
[français](README.fr-FR.md).
57

src/algorithms/math/factorial/README.zh-CN.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
5! = 5 * 4 * 3 * 2 * 1 = 120
77
```
88

9-
| n | n! |
9+
| n | n! |
1010
| ----- | --------------------------: |
1111
| 0 | 1 |
1212
| 1 | 1 |
@@ -25,3 +25,8 @@
2525
| 14 | 87 178 291 200 |
2626
| 15 | 1 307 674 368 000 |
2727

28+
实现方式
29+
30+
- 迭代方式 iter
31+
- 递归方式 recurse
32+
- 尾递归优化 tail

src/algorithms/math/fast-powering/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Fast Powering Algorithm
22

3+
快速算次方
4+
35
_Read this in other languages:_
46
[français](README.fr-FR.md).
57

src/algorithms/math/fibonacci/README.zh-CN.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# 斐波那契数
22

3+
斐波那契数 - `经典``闭式` 版本
4+
35
_Read this in other languages:_
46
[français](README.fr-FR.md),
57
[english](README.md),

src/algorithms/math/fourier-transform/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
_Read this in other languages:_
44
[français](README.fr-FR.md).
55

6+
离散傅里叶变换 - 把时间信号解析成构成它的频率
7+
68
## Definitions
79

810
The **Fourier Transform** (**FT**) decomposes a function of time (a signal) into

src/algorithms/math/integer-partition/README.md

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Integer Partition
22

3-
In number theory and combinatorics, a partition of a positive
4-
integer `n`, also called an **integer partition**, is a way of
5-
writing `n` as a sum of positive integers.
3+
整数拆分
64

7-
Two sums that differ only in the order of their summands are
8-
considered the same partition. For example, `4` can be partitioned
5+
In number theory and combinatorics, a partition of a positive
6+
integer `n`, also called an **integer partition**, is a way of
7+
writing `n` as a sum of positive integers.
8+
9+
Two sums that differ only in the order of their summands are
10+
considered the same partition. For example, `4` can be partitioned
911
in five distinct ways:
1012

1113
```
@@ -17,13 +19,13 @@ in five distinct ways:
1719
```
1820

1921
The order-dependent composition `1 + 3` is the same partition
20-
as `3 + 1`, while the two distinct
21-
compositions `1 + 2 + 1` and `1 + 1 + 2` represent the same
22+
as `3 + 1`, while the two distinct
23+
compositions `1 + 2 + 1` and `1 + 1 + 2` represent the same
2224
partition `2 + 1 + 1`.
2325

2426
Young diagrams associated to the partitions of the positive
25-
integers `1` through `8`. They are arranged so that images
26-
under the reflection about the main diagonal of the square
27+
integers `1` through `8`. They are arranged so that images
28+
under the reflection about the main diagonal of the square
2729
are conjugate partitions.
2830

2931
![Integer Partition](https://upload.wikimedia.org/wikipedia/commons/d/d8/Ferrer_partitioning_diagrams.svg)

src/algorithms/math/is-power-of-two/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# Is a power of two
22

3+
判断 2 次方数 - 检查数字是否为 2 的幂 (原生和按位算法)
4+
35
Given a positive integer, write a function to find if it is
46
a power of two or not.
57

src/algorithms/math/least-common-multiple/README.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
# Least common multiple
22

3-
In arithmetic and number theory, the least common multiple,
4-
lowest common multiple, or smallest common multiple of
5-
two integers `a` and `b`, usually denoted by `LCM(a, b)`, is
6-
the smallest positive integer that is divisible by
7-
both `a` and `b`. Since division of integers by zero is
8-
undefined, this definition has meaning only if `a` and `b` are
3+
最小公倍数 (LCM)
4+
5+
In arithmetic and number theory, the least common multiple,
6+
lowest common multiple, or smallest common multiple of
7+
two integers `a` and `b`, usually denoted by `LCM(a, b)`, is
8+
the smallest positive integer that is divisible by
9+
both `a` and `b`. Since division of integers by zero is
10+
undefined, this definition has meaning only if `a` and `b` are
911
both different from zero. However, some authors define `lcm(a,0)`
1012
as `0` for all `a`, which is the result of taking the `lcm`
1113
to be the least upper bound in the lattice of divisibility.
@@ -26,20 +28,20 @@ and the multiples of `6` are:
2628
6, 12, 18, 24, 30, 36, 42, 48, 54, 60, 66, 72, ...
2729
```
2830

29-
Common multiples of `4` and `6` are simply the numbers
31+
Common multiples of `4` and `6` are simply the numbers
3032
that are in both lists:
3133

3234
```
3335
12, 24, 36, 48, 60, 72, ....
3436
```
3537

36-
So, from this list of the first few common multiples of
38+
So, from this list of the first few common multiples of
3739
the numbers `4` and `6`, their least common multiple is `12`.
3840

3941
## Computing the least common multiple
4042

41-
The following formula reduces the problem of computing the
42-
least common multiple to the problem of computing the greatest
43+
The following formula reduces the problem of computing the
44+
least common multiple to the problem of computing the greatest
4345
common divisor (GCD), also known as the greatest common factor:
4446

4547
```
@@ -48,11 +50,11 @@ lcm(a, b) = |a * b| / gcd(a, b)
4850

4951
![LCM](https://upload.wikimedia.org/wikipedia/commons/c/c9/Symmetrical_5-set_Venn_diagram_LCM_2_3_4_5_7.svg)
5052

51-
A Venn diagram showing the least common multiples of
52-
combinations of `2`, `3`, `4`, `5` and `7` (`6` is skipped as
53+
A Venn diagram showing the least common multiples of
54+
combinations of `2`, `3`, `4`, `5` and `7` (`6` is skipped as
5355
it is `2 × 3`, both of which are already represented).
5456

55-
For example, a card game which requires its cards to be
57+
For example, a card game which requires its cards to be
5658
divided equally among up to `5` players requires at least `60`
5759
cards, the number at the intersection of the `2`, `3`, `4`
5860
and `5` sets, but not the `7` set.

src/algorithms/math/liu-hui/README.md

Lines changed: 36 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,66 +1,68 @@
11
# Liu Hui's π Algorithm
22

3+
割圆术 - 基于 N-gons 的近似 π 计算
4+
35
Liu Hui remarked in his commentary to The Nine Chapters on the Mathematical Art,
4-
that the ratio of the circumference of an inscribed hexagon to the diameter of
5-
the circle was `three`, hence `π` must be greater than three. He went on to provide
6-
a detailed step-by-step description of an iterative algorithm to calculate `π` to
7-
any required accuracy based on bisecting polygons; he calculated `π` to
8-
between `3.141024` and `3.142708` with a 96-gon; he suggested that `3.14` was
9-
a good enough approximation, and expressed `π` as `157/50`; he admitted that
10-
this number was a bit small. Later he invented an ingenious quick method to
11-
improve on it, and obtained `π ≈ 3.1416` with only a 96-gon, with an accuracy
12-
comparable to that from a 1536-gon. His most important contribution in this
6+
that the ratio of the circumference of an inscribed hexagon to the diameter of
7+
the circle was `three`, hence `π` must be greater than three. He went on to provide
8+
a detailed step-by-step description of an iterative algorithm to calculate `π` to
9+
any required accuracy based on bisecting polygons; he calculated `π` to
10+
between `3.141024` and `3.142708` with a 96-gon; he suggested that `3.14` was
11+
a good enough approximation, and expressed `π` as `157/50`; he admitted that
12+
this number was a bit small. Later he invented an ingenious quick method to
13+
improve on it, and obtained `π ≈ 3.1416` with only a 96-gon, with an accuracy
14+
comparable to that from a 1536-gon. His most important contribution in this
1315
area was his simple iterative `π` algorithm.
1416

1517
## Area of a circle
1618

1719
Liu Hui argued:
1820

19-
> Multiply one side of a hexagon by the radius (of its
20-
circumcircle), then multiply this by three, to yield the
21-
area of a dodecagon; if we cut a hexagon into a
22-
dodecagon, multiply its side by its radius, then again
23-
multiply by six, we get the area of a 24-gon; the finer
24-
we cut, the smaller the loss with respect to the area
25-
of circle, thus with further cut after cut, the area of
26-
the resulting polygon will coincide and become one with
21+
> Multiply one side of a hexagon by the radius (of its
22+
circumcircle), then multiply this by three, to yield the
23+
area of a dodecagon; if we cut a hexagon into a
24+
dodecagon, multiply its side by its radius, then again
25+
multiply by six, we get the area of a 24-gon; the finer
26+
we cut, the smaller the loss with respect to the area
27+
of circle, thus with further cut after cut, the area of
28+
the resulting polygon will coincide and become one with
2729
the circle; there will be no loss
2830

2931
![Liu Hui](https://upload.wikimedia.org/wikipedia/commons/6/69/Cutcircle2.svg)
3032

3133
Liu Hui's method of calculating the area of a circle.
3234

33-
Further, Liu Hui proved that the area of a circle is half of its circumference
35+
Further, Liu Hui proved that the area of a circle is half of its circumference
3436
multiplied by its radius. He said:
3537

36-
> Between a polygon and a circle, there is excess radius. Multiply the excess
37-
radius by a side of the polygon. The resulting area exceeds the boundary of
38+
> Between a polygon and a circle, there is excess radius. Multiply the excess
39+
radius by a side of the polygon. The resulting area exceeds the boundary of
3840
the circle
3941

40-
In the diagram `d = excess radius`. Multiplying `d` by one side results in
41-
oblong `ABCD` which exceeds the boundary of the circle. If a side of the polygon
42-
is small (i.e. there is a very large number of sides), then the excess radius
42+
In the diagram `d = excess radius`. Multiplying `d` by one side results in
43+
oblong `ABCD` which exceeds the boundary of the circle. If a side of the polygon
44+
is small (i.e. there is a very large number of sides), then the excess radius
4345
will be small, hence excess area will be small.
4446

45-
> Multiply the side of a polygon by its radius, and the area doubles;
47+
> Multiply the side of a polygon by its radius, and the area doubles;
4648
hence multiply half the circumference by the radius to yield the area of circle.
4749

4850
![Liu Hui](https://upload.wikimedia.org/wikipedia/commons/9/95/Cutcircle.svg)
4951

50-
The area within a circle is equal to the radius multiplied by half the
52+
The area within a circle is equal to the radius multiplied by half the
5153
circumference, or `A = r x C/2 = r x r x π`.
5254

5355
## Iterative algorithm
5456

55-
Liu Hui began with an inscribed hexagon. Let `M` be the length of one side `AB` of
57+
Liu Hui began with an inscribed hexagon. Let `M` be the length of one side `AB` of
5658
hexagon, `r` is the radius of circle.
5759

5860
![Liu Hui](https://upload.wikimedia.org/wikipedia/commons/4/46/Liuhui_geyuanshu.svg)
5961

60-
Bisect `AB` with line `OPC`, `AC` becomes one side of dodecagon (12-gon), let
62+
Bisect `AB` with line `OPC`, `AC` becomes one side of dodecagon (12-gon), let
6163
its length be `m`. Let the length of `PC` be `j` and the length of `OP` be `G`.
6264

63-
`AOP`, `APC` are two right angle triangles. Liu Hui used
65+
`AOP`, `APC` are two right angle triangles. Liu Hui used
6466
the [Gou Gu](https://en.wikipedia.org/wiki/Pythagorean_theorem) (Pythagorean theorem)
6567
theorem repetitively:
6668

@@ -79,12 +81,12 @@ theorem repetitively:
7981
![](https://wikimedia.org/api/rest_v1/media/math/render/svg/3ffeafe88d2983b364ad3442746063e3207fe842)
8082

8183

82-
From here, there is now a technique to determine `m` from `M`, which gives the
83-
side length for a polygon with twice the number of edges. Starting with a
84-
hexagon, Liu Hui could determine the side length of a dodecagon using this
85-
formula. Then continue repetitively to determine the side length of a
86-
24-gon given the side length of a dodecagon. He could do this recursively as
87-
many times as necessary. Knowing how to determine the area of these polygons,
84+
From here, there is now a technique to determine `m` from `M`, which gives the
85+
side length for a polygon with twice the number of edges. Starting with a
86+
hexagon, Liu Hui could determine the side length of a dodecagon using this
87+
formula. Then continue repetitively to determine the side length of a
88+
24-gon given the side length of a dodecagon. He could do this recursively as
89+
many times as necessary. Knowing how to determine the area of these polygons,
8890
Liu Hui could then approximate `π`.
8991

9092
## References

0 commit comments

Comments
 (0)