-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ac31c6
commit 6684ccb
Showing
4 changed files
with
37 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Notes for 01_binary_search | ||
1. **二分查找(binary search)**算法:输入是一个有序列表,如果查找元素包含其中,则返回其位置;否则,返回null。**简单查找**则是从第一个序号开始往后比较。 | ||
2. 对于包含$n$个元素的列表,用二分查找最多需要$log_2 n$步(对数时间),而简单查找最多需要$n$步(线性时间)。 | ||
3. 大O表示法让我们可以比较操作数,也展示了算法运行时间随列表长度的增速。 | ||
4. 大O表示法说的是最糟的情形。 | ||
5. 旅行商问题的时间复杂度是$O(n!)$(阶乘时间)。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,11 @@ | ||
# Notes for selection sort | ||
1. 选择排序是,每次找到数组中最小的值,附到一个新数组中,从而形成一个有序排列的新数组。 | ||
# Notes for 02_selection_sort | ||
|
||
## 数组和链表 | ||
1. **数组**占用地址连续的内存单元,内存大小需要事先决定。 | ||
2. **链表**中每个元素都存储下一个元素的内存地址,将一系列随机的内存单元穿在一起。 | ||
3. 链表只支持顺序访问,而数组支持随机访问。 | ||
4. 要进行增删操作,链表是更好的选择;要读取速度快,数组更合适。 | ||
|
||
## 选择排序 | ||
1. **选择排序**算法:每次遍历数组找到其中的最小值,添加到一个新数组中,从而形成一个有序排列的新数组。 | ||
2. 选择排序时间复杂度为$O(n^2)$。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,27 @@ | ||
# Notes for 04_quicksort | ||
|
||
## 分治法 | ||
1. 分治法(Devide and conquer, D&G)是递归式的问题解决方法。 | ||
2. 分治法解决问题分为2步: | ||
2. **分治法**解决问题分为2步: | ||
1. 找出简单的基线条件(base case); | ||
2. 不断将缩小问题的规模,直到符合基线条件。 | ||
3. 欧几里得算法 | ||
3. 欧几里得算法(找出最大方块)。 | ||
4. 编写涉及数组的递归函数时,基线条件通常是数组为空或只包含一个元素。 | ||
5. 函数式编程语言Haskell中没有循环语句,递归就起了很重要的作用。(递归可以替代循环?) | ||
6. 判断语句的条件中不应包含递归成分,为避免出错,过程中应尽量不要改变列表(pop,remove) | ||
5. 函数式编程语言Haskell中没有循环语句,递归就起了很重要的作用(递归可以替代循环)。 | ||
6. 判断语句的条件中不应包含递归成分(可能导致超过recursion limit);为避免出错,过程中应尽量不要改变列表(pop,remove)。 | ||
7. 二分查找也是一种分治法,基线条件是数组中含0个元素(low>high),递归条件则是数组中至少含有一个元素(low<=high)。 | ||
|
||
## 快速排序 | ||
1. 快速排序的基本步骤: | ||
1. **快速排序**的基本步骤: | ||
- 选择基准值(pivot); | ||
- 对数组进行分区(partitioning),分成大于基准值的元素和小于基准值的元素,及基准值本身; | ||
- 对两边的分区进行快速排序(分治D&C)。 | ||
2. 归纳证明(通常与分治法协同合作)分为两步: | ||
- 基线条件(对0元素或一个元素的数组管用); | ||
- 归纳条件(如果对一个元素数组管用,对2个元素的数组也管用)。 | ||
|
||
## 再谈大O表示法 | ||
1. 实际的运行时间是$c*n$,其中$c$是一次操作花费的固定时间常量,而$n$则表示操作数。**在大O运行时间不同时,常量不重要**(增速的重要性远超过常量);在大O运行时间相同时,才考虑常量,因此快速排序和合并排序大O时间一致($O(log n)$),快速排序时间常量小,故而选择快速排序。 | ||
2. 快速排序的大O运行时间分为平均情况和最糟情况,遇上平均情况的概率比最糟情况大得多。**大O运行时间=调用栈高*每层的运行时间。** | ||
- 平均情况(最佳情况):每次都随机选择一个元素做基准值,$O(log n)*O(n) = O(nlog n)$; | ||
- 最糟情况:有序数组的第一个元素作为基准值,$O(n)*O(n) = O(n^2)$,与第一章的大O表示最糟情况不一样,这里的最糟情况不能代表大多数。 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,21 @@ | ||
# Grokking Algorithms Code | ||
Code to implement the algorithms mentioned in book *grokking algorithms*. | ||
|
||
## Coding language | ||
Code to implement the algorithms mentioned in book [Grokking Algorithms](https://www.manning.com/bhargava). | ||
|
||
I took reference from the [official repository](https://github.com/egonSchiele/grokking_algorithms) to name my folders. | ||
|
||
## Coding Language | ||
|
||
- Python 2.7 | ||
|
||
## Algorithms List | ||
|
||
- [x] 01.Binary search. | ||
- [x] 02.Selection sort. | ||
- [x] 03.Recursion. | ||
- [x] 04.Quicksort. | ||
|
||
## Notice | ||
- Under the folder of each chapter includes a `README.md` file that records my notes for that chapter, and is written in Chinese(每章文件夹中都包含有中文写的笔记). | ||
|
||
- Under the folder of each chapter includes a `README.md` file that records my notes for that chapter, and is written in Chinese(每章文件夹中都包含有中文笔记)。 | ||
- My implementation might be a bit diffrent from that in book, but it runs okay and share same structures as the authentic one. |