Skip to content

Commit a677dd1

Browse files
committed
fix: modify readme 🍎
1 parent 76271d6 commit a677dd1

File tree

2 files changed

+186
-86
lines changed

2 files changed

+186
-86
lines changed

README-EN.md

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
<h1 align="center">:whale: 用 PHP 的方式实现的各类算法合集 :whale: </h1>
2+
3+
<p align="center">
4+
<a href="https://github.com/PuShaoWei/arithmetic-php#简易结构">
5+
<img src="https://img.shields.io/badge/php-done-brightgreen.svg" alt="php">
6+
</a>
7+
<a href="https://github.com/PuShaoWei/arithmetic-php">
8+
<img src="https://img.shields.io/github/issues-pr-raw/arithmetic-php/cdnjs.svg">
9+
</a>1
10+
<a href="https://github.com/PuShaoWei/arithmetic-php">
11+
<img src="https://img.shields.io/codacy/grade/e27821fb6289410b8f58338c7e0bc686.svg">
12+
</a>
13+
<a href="https://github.com/PuShaoWei/arithmetic-php">
14+
<img src="https://img.shields.io/travis/rust-lang/rust.svg">
15+
</a>
16+
<a href="https://github.com/PuShaoWei/arithmetic-php">
17+
<img src="https://img.shields.io/github/license/mashape/apistatus.svg">
18+
</a>
19+
</p>
20+
<p align="center"> <a href="./README.md">中文版</a> <p>
21+
22+
23+
## 简易结构
24+
25+
├──Package
26+
│ ├── Sort
27+
│ │ ├── BubbleSort.php
28+
│ │ ├── QuickSort.php
29+
│ │ ├── ShellSort.php
30+
│ │ ├── MergeSort.php
31+
│ │ ├── InsertSort.php
32+
│ │ └── SelectSort.php
33+
│ │
34+
│ ├── Query 查找篇
35+
│ │ ├── BinaryQuery.php
36+
│ │ ├── InseertQuery.php
37+
│ │ ├── FibonacciQuery.php
38+
│ │ └── QulickQuery.php
39+
│ │
40+
│ └── Other 其他
41+
│ ├── MonkeyKing.php
42+
│ ├── DynamicProgramming.php
43+
│ ├── Fibonacci.php
44+
│ ├── StealingApples.php
45+
│ ├── HanoiGames.php
46+
│ ├── ColorBricks.php
47+
│ ├── GetCattle.php
48+
│ ├── OnlyNumbers.php
49+
│ └── BigSmallReplace.php
50+
51+
├──LICENSE
52+
└──README.md
53+
54+
## What to do?
55+
To record their understanding algorithms, data structure, the process of simple comprehensive and detailed as possible, let the learning algorithm using flexible, refueling(ง •̀_•́)ง
56+
57+
## logarithmic
58+
log<sub>10</sub>100 It's equivalent to saying, "how many tens do you multiply?" the answer is, of course, two
59+
so log<sub>10</sub>100=2,The logarithmic operation is the inverse of the power operation
60+
61+
left|right
62+
---|---
63+
2<sup>3</sup> = 8 | log<sub>2</sub>8 = 3
64+
2<sup>4</sup> = 16 | log<sub>2</sub>16 = 4
65+
2<sup>5</sup> = 32 | log<sub>2</sub>32 = 5
66+
67+
If you don't, we won't wait for you
68+
69+
## The elapsed time
70+
Take binary search for example, how much time can you save by using it? Simply look for the Numbers and if the list contains 100 Numbers, you need to guess 100 times.
71+
In other words, the number of guesses is the same as the length of the list, which is called linear time, while binary search is different if the list contains 100 elements
72+
It takes up to seven times, and if the list contains four billion digits, it should be guessed 32 times, while the running time of the subsearch is logarithmic time `O(log)`
73+
74+
## Big O notation
75+
The big O notation is a special representation of how fast the algorithm can be. There's a diaosi. In fact, you often have to copy other people's code.
76+
In this case, you know how fast these algorithms are
77+
78+
- The running time of the algorithm increases at different speeds
79+
- For example, the difference between a simple find and a binary search
80+
element|Easy to find|Binary search
81+
---|---|---
82+
100|100ms|7ms
83+
10000|10s|14ms
84+
1 000 000 000 |11day|30ms
85+
- ` O ` said hair is pointed out that how fast algorithms, such as list contains ` n ` element, a simple search need to check each element, so you need to perform ` n ` time operations
86+
Using large ` O ` said ` O (n) to make this operation `, binary search need to perform log<sub>n</sub> using large ` O ` said to`O(log n)`
87+
- Some common big O runtime
88+
- O(log n) ,It's also called log time, and this algorithm includes binary algorithms
89+
- O(n),Also known as linear time, this algorithm includes simple lookups.
90+
- O(n * log n) Quick sort
91+
- O(n<sub>2</sub>),Selection sort
92+
- O(n!) Factorial time
93+
- Here is the point
94+
- The speed of the algorithm is not the time, but the growth of operands
95+
- When we talk about the speed of the algorithm, what we're talking about is how fast will it run as the input increases
96+
- The running time of the algorithm is expressed in large O notation
97+
- O(log n) is faster than O (n), and the more elements that need to be searched, the more the former is faster than the latter
98+
99+
100+
## A simple comparison of recursion and loops:
101+
102+
1. From a procedural point of view, the recursion manifests itself as calling itself, and the loop does not have this form.
103+
2. Recursive proceed from the ultimate goal of the problem, and gradually to a complex problem into a simple problem, and simple question solution and complicated problem, at the same time the presence of the benchmark, can eventually get a problem, is the reverse. And the circulation is from the simple question, step by step forward development, finally get the question, is positive.
104+
3. Any cycle can be represented by recursion, but it is necessary to use the loop to achieve recursion (except for one-way recursion and tail recursion), and the stack structure must be introduced to stack the stack.
105+
4.In general, non-recursive efficiency is higher than recursion. And recursive function calls are expensive and recursive times are limited by stack size.
106+
107+
## Progressive learning
108+
1. Fork 我的项目并提交你的 `idea`
109+
2. Pull Request
110+
3. Merge
111+
112+
## 纠错
113+
If you find something wrong, you can initiate a [issue](https://github.com/PuShaoWei/designPatterns-go/issues)or [pull request](https://github.com/PuShaoWei/designPatterns-go/pulls),I will correct it in time
114+
115+
> 补充:发起pull request的commit message请参考文章[Commit message 和 Change log 编写指南](http://www.ruanyifeng.com/blog/2016/01/commit_message_change_log.html)
116+
117+
## Contributors
118+
119+
Thanks for the issue or pull request of the following friends:
120+
121+
- [hailwood ](https://github.com/hailwood)
122+
- [zhangxuanru](https://github.com/zhangxuanru)
123+
- [ifreesec](https://github.com/ifreesec)
124+
- [openset](https://github.com/openset)
125+
126+
## License
127+
MIT

README.md

Lines changed: 59 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
</a>
77
<a href="https://github.com/PuShaoWei/arithmetic-php">
88
<img src="https://img.shields.io/github/issues-pr-raw/arithmetic-php/cdnjs.svg">
9-
</a>
9+
</a>1
1010
<a href="https://github.com/PuShaoWei/arithmetic-php">
1111
<img src="https://img.shields.io/codacy/grade/e27821fb6289410b8f58338c7e0bc686.svg">
1212
</a>
@@ -17,12 +17,9 @@
1717
<img src="https://img.shields.io/github/license/mashape/apistatus.svg">
1818
</a>
1919
</p>
20+
<p align="center"> <a href="./README-EN.md">English</a> <p>
2021

2122

22-
## About
23-
24-
> 如果说各种编程语言是程序员的招式,那么数据结构和算法就相当于程序员的内功。
25-
2623
## 简易结构
2724
2825
├──Package
@@ -53,91 +50,64 @@
5350
5451
├──LICENSE
5552
└──README.md
56-
57-
## 时间复杂度
58-
59-
> #### 时间频度
60-
一个算法执行所耗费的时间,从理论上是不能算出来的,必须上机运行测试才能知道。
61-
但我们不可能也没有必要对每个算法都上机测试,只需知道哪个算法花费的时间多,哪个算法花费的时间少就可以了。
62-
并且一个算法花费的时间与算法中语句的执行次数成正比例,哪个算法中语句执行次数多,它花费时间就多。
63-
一个算法中的语句执行次数称为语句频度或时间频度。记为 `T(n)`。
64-
65-
> #### 时间复杂度
66-
在刚才提到的时间频度中,n称为问题的规模,当n不断变化时,时间频度T(n)也会不断变化。
67-
但有时我们想知道它变化时呈现什么规律。为此,我们引入时间复杂度概念。
68-
一般情况下,算法中基本操作重复执行的次数是问题规模n的某个函数,用T(n)表示,
69-
若有某个辅助函数f(n),使得当n趋近于无穷大时,T(n)/f(n)的极限值为不等于零的常数,则称f(n)是T(n)的同数量级函数。
70-
记作T(n)=O(f(n)),称O(f(n)) 为算法的渐进时间复杂度,简称时间复杂度。时间频度不同,但时间复杂度可能相同。
71-
如:T(n)=n2+3n+4与T(n)=4n2+2n+1它们的频度不同,但时间复杂度相同,都为O(n2)。
72-
按数量级递增排列,常见的时间复杂度有:常数阶O(1),对数阶O(log2n),线性阶O(n), 线性对数阶O(nlog2n),平方阶O(n2),立方阶O(n3),..., k次方阶O(nk),指数阶O(2n)。
73-
随着问题规模n的不断增大,上述时间复杂度不断增大,算法的执行效率越低。
74-
75-
> #### 最坏时间复杂度和平均时间复杂度  
76-
最坏情况下的时间复杂度称最坏时间复杂度。一般不特别说明,讨论的时间复杂度均是最坏情况下的时间复杂度。
77-
这样做的原因是:最坏情况下的时间复杂度是算法在任何输入实例上运行时间的上界,这就保证了算法的运行时间不会比任何更长。
78-
在最坏情况下的时间复杂度为T(n)=0(n),它表示对于任何输入实例,该算法的运行时间不可能大于0(n)。
79-
平均时间复杂度是指所有可能的输入实例均以等概率出现的情况下,算法的期望运行时间。指数阶0(2n),显然,时间复杂度为指数阶0(2n)的算法效率极低,当n值稍大时就无法应用。
80-
81-
> #### 求时间复杂度
82-
> 如果算法的执行时间不随着问题规模n的增加而增长,即使算法中有上千条语句,其执行时间也不过是一个较大的常数。此类算法的时间复杂度是O(1)。
83-
```
84-
x=91; y=100;
85-
while(y>0) if(x>100) {x=x-10;y--;} else x++;
86-
```
87-
解答: T(n)=O(1),
88-
这个程序看起来有点吓人,总共循环运行了1100次,但是我们看到n没有?
89-
没。这段程序的运行是和n无关的,
90-
就算它再循环一万年,我们也不管他,只是一个常数阶的函数
91-
- 当有若干个循环语句时,算法的时间复杂度是由嵌套层数最多的循环语句中最内层语句的频度f(n)决定的。
92-
```
93-
x=1;
94-
for(i=1;i<=n;i++)
95-
for(j=1;j<=i;j++)
96-
for(k=1;k<=j;k++)
97-
x++;   
98-
```
99-
该程序段中频度最大的语句是(5),内循环的执行次数虽然与问题规模n没有直接关系,但是却与外层循环的变量取值有关,而最外层循环的次数直接与n有关,
100-
因此可以从内层循环向外层分析语句(5)的执行次数: 则该程序段的时间复杂度为T(n)=O(n3/6+低次项)=O(n3)
101-
- 算法的时间复杂度不仅仅依赖于问题的规模,还与输入实例的初始状态有关。
102-
在数值A[0..n-1]中查找给定值K的算法大致如下:
103-
```
104-
i=n-1;
105-
while(i>=0&&(A[i]!=k))
106-
i--;
107-
return i;
108-
```
109-
此算法中的语句(3)的频度不仅与问题规模n有关,还与输入实例中A的各元素取值及K的取值有关:
110-
- ①若A中没有与K相等的元素,则语句(3)的频度f(n)=n;
111-
- ②若A的最后一个元素等于K, 则语句(3)的频度f(n)是常数0。
112-
113-
- 时间复杂度评价性能
114-
有两个算法A1和A2求解同一问题,时间复杂度分别是T1(n)=100n2,T2(n)=5n3。
115-
(1)当输入量n<20时,有T1(n)>T2(n),后者花费的时间较少。
116-
(2)随着问题规模n的增大,两个算法的时间开销之比5n3/100n2=n/20亦随着增大。即当问题规模较大时,算法A1比算法A2要有效地多。
117-
它们的渐近时间复杂度O(n2)和O(n3)从宏观上评价了这两个算法在时间方面的质量。
118-
在算法分析时,往往对算法的时间复杂度和渐近时间复杂度不予区分,而经常是将渐近时间复杂度T(n)=O(f(n))简称为时间复杂度,
119-
其中的f(n)一般是算法中频度最大的语句频度。
120-
121-
## 空间复杂度
122-
一个程序的空间复杂度是指运行完一个程序所需内存的大小。
123-
利用程序的空间复杂度,可以对程序的运行所需要的内存多少有个预先估计。一个程序执行时除了需要存储空间和存储本身所使用的指令、常数、变量和输入数据外,
124-
还需要一些对数据进行操作的工作单元和存储一些为现实计算所需信息的辅助空间。程序执行时所需存储空间包括以下两部分。  
125-
> #### 固定部分
126-
这部分空间的大小与输入/输出的数据的个数多少、数值无关。主要包括指令空间(即代码空间)、数据空间(常量、简单变量)等所占的空间。这部分属于静态空间。
127-
> #### 可变空间,这部分空间的主要包括动态分配的空间,以及递归栈所需的空间等。这部分的空间大小与算法有关。
128-
一个算法所需的存储空间用f(n)表示。S(n)=O(f(n))  其中n为问题的规模,S(n)表示空间复杂度。
53+
54+
## 要做什么?
55+
记录自己理解算法,数据结构的过程,尽可能的简单全面以及详细,让算法学习运用灵活自如,加油(ง •̀_•́)ง
56+
57+
## 对数
58+
log<sub>10</sub>100 相当于问"降多少个10相乘的结果为100",答案当然是2个了
59+
因此log<sub>10</sub>100=2,即对数运算是幂运算的逆运算
60+
61+
left|right
62+
---|---
63+
2<sup>3</sup> = 8 | log<sub>2</sub>8 = 3
64+
2<sup>4</sup> = 16 | log<sub>2</sub>16 = 4
65+
2<sup>5</sup> = 32 | log<sub>2</sub>32 = 5
66+
67+
就是酱紫,你要是没有学会,我们也不会等你
68+
69+
## 运行时间
70+
以二分查找为例,使用它可节省多少时间呢?简单查找诸葛地检查数字,如果列表包含100个数字,最多需要猜100次。
71+
换而言之嘴多需要猜测的次数与列表长度相同,这被称为线性时间(linear time),而二分查找则不同,如果列表包含100个元素
72+
最多需要7次,如果列表包含40亿个数字,最多需猜32次,而分查找的运行时间为对数时间 `O(log)`
73+
74+
## 大O表示法
75+
大O表示法是一种特殊的表示法 ,指出了算法的速度有多快。有个屌用啊,实际上,你经常要去复制别人的代码。
76+
在这种情况下,知道这些算法的速度有快有慢
77+
78+
- 算法的运行时间以不同的速度增加
79+
- 例如简单查找与二分查找的区别
80+
元素|简单查找|二分查找
81+
---|---|---
82+
100个元素|100ms|7ms
83+
10000个元素|10s|14ms
84+
1 000 000 000 个元素|11天|30ms
85+
-`O`表示发指出了算法有多快,例如列表包含`n`个元素,简单查找需要检查每个元素,因此需要执行`n`次操作
86+
使用大`O`表示发这个运行时间为`O(n)`,二分查找需要执行log<sub>n</sub>次操作,使用大`O`表示为`O(log n)`
87+
- 一些常见的大O运行时间
88+
- O(log n) ,也叫对数时间,这样的算法包括二分算法
89+
- O(n),也叫线性时间,这样的算法包括简单查找。
90+
- O(n * log n) 快速排序
91+
- O(n<sub>2</sub>),选择排序
92+
- O(n!) 即阶乘时间
93+
- 这里是重点
94+
- 算法的速度指的并非时间,而是操作数的增速
95+
- 谈论算法的速度时间时,我们说的是随着输入的增加,其运行时间将以什么样的速度增加
96+
- 算法的运行时间用大O表示发表示
97+
- O(log n)比O(n)快,当需要搜索的元素越多时,前者比后者快的越多
98+
12999

130100
## 递归和循环的简单比较:
131-
1、从程序上看,递归表现为自己调用自己,循环则没有这样的形式。
132-
2、递归是从问题的最终目标出发,逐渐将复杂问题化为简单问题,并且简单的问题的解决思路和复杂问题一样,同时存在基准情况,就能最终求得问题,是逆向的。而循环是从简单问题出发,一步步的向前发展,最终求得问题,是正向的。
133-
3、任意循环都是可以用递归来表示的,但是想用循环来实现递归(除了单向递归和尾递归),都必须引入栈结构进行压栈出栈。
134-
4、一般来说,非递归的效率高于递归。而且递归函数调用是有开销的,递归的次数受堆栈大小的限制。
135101

102+
1. 从程序上看,递归表现为自己调用自己,循环则没有这样的形式。
103+
2. 递归是从问题的最终目标出发,逐渐将复杂问题化为简单问题,并且简单的问题的解决思路和复杂问题一样,同时存在基准情况,就能最终求得问题,是逆向的。而循环是从简单问题出发,一步步的向前发展,最终求得问题,是正向的。
104+
3. 任意循环都是可以用递归来表示的,但是想用循环来实现递归(除了单向递归和尾递归),都必须引入栈结构进行压栈出栈。
105+
4. 一般来说,非递归的效率高于递归。而且递归函数调用是有开销的,递归的次数受堆栈大小的限制。
136106

137107
## 一起进步学习
138-
1. Fork 我的项目并提交你的 `idea`
139-
2. Pull Request
140-
3. Merge
108+
1. Fork 我的项目并提交你的 `idea`
109+
2. Pull Request
110+
3. Merge
141111

142112
## 纠错
143113

@@ -150,4 +120,7 @@ return i;
150120
- [hailwood ](https://github.com/hailwood)
151121
- [zhangxuanru](https://github.com/zhangxuanru)
152122
- [ifreesec](https://github.com/ifreesec)
153-
- [openset](https://github.com/openset)
123+
- [openset](https://github.com/openset)
124+
125+
## License
126+
MIT

0 commit comments

Comments
 (0)