-
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
Showing
14 changed files
with
285 additions
and
5 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
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
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 |
---|---|---|
|
@@ -5,5 +5,3 @@ | |
::: | ||
|
||
- [x] [二叉树](./binaryTree/README.md) | ||
|
||
- [x] [排序](./sort/README.md) |
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,25 @@ | ||
## 排序算法(Javascript) | ||
|
||
::: tip | ||
主要是对一些常见的 **排序算法** 学习并实现 | ||
::: | ||
|
||
- [x] [冒泡排序](./bubbleSort/README.md) | ||
|
||
- [x] [桶排序](./bucketSort/README.md) | ||
|
||
- [x] [计数排序](./countingSort/README.md) | ||
|
||
- [x] [堆排序](./heapSort/README.md) | ||
|
||
- [x] [插入排序](./insertionSort/README.md) | ||
|
||
- [x] [归并排序](./mergeSort/README.md) | ||
|
||
- [x] [快速排序](./quickSort/README.md) | ||
|
||
- [x] [基数排序](./radixSort/README.md) | ||
|
||
- [x] [选择排序](./selectionSort/README.md) | ||
|
||
- [x] [希尔排序](./shellSort/README.md) |
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,23 @@ | ||
## 原理 | ||
|
||
冒泡排序(`Bubble Sort`)也是一种简单直观的排序算法。它重复地走访过要排序的数列,一次比较两个元素,如果他们的顺序错误就把他们交换过来。走访数列的工作是重复地进行直到没有再需要交换,也就是说该数列已经排序完成。这个算法的名字由来是因为越小的元素会经由交换慢慢“浮”到数列的顶端。 | ||
|
||
## 算法步骤 | ||
|
||
- 比较相邻的元素。如果第一个比第二个大,就交换他们两个。 | ||
|
||
- 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对。这步做完后,最后的元素会是最大的数。 | ||
|
||
- 针对所有的元素重复以上的步骤,除了最后一个。 | ||
|
||
- 持续每次对越来越少的元素重复上面的步骤,直到没有任何一对数字需要比较。 | ||
|
||
![bubbleSort](~@images/src/algorithms/sort/bubbleSort/images/bubbleSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/bubbleSort/index.ts | ||
|
||
## 参考 | ||
|
||
[bubbleSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/1.bubbleSort.md) |
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,29 @@ | ||
## 原理 | ||
|
||
桶排序是计数排序的升级版。它利用了函数的映射关系, 高效与否的关键就在于这个映射函数的确定。为了使桶排序更加高效, 我们需要做到这两点: | ||
|
||
- 在额外空间充足的情况下, 尽量增大桶的数量 | ||
- 使用的映射函数能够将输入的 `N` 个数据均匀的分配到 `K` 个桶中 | ||
|
||
同时, 对于桶中元素的排序, 选择何种比较排序算法对于性能的影响至关重要。 | ||
|
||
- 什么时候最快 | ||
当输入的数据可以均匀的分配到每一个桶中。 | ||
- 什么时候最慢 | ||
当输入的数据被分配到了同一个桶中。 | ||
|
||
## 算法步骤 | ||
|
||
![bucketSort](~@images/src/algorithms/sort/bucketSort/images/bucketSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/bucketSort/index.ts | ||
|
||
## 参考 | ||
|
||
[bucketSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/9.bucketSort.md) | ||
|
||
[Data Structure Visualizations: BucketSort](https://www.cs.usfca.edu/~galles/visualization/BucketSort.html) | ||
|
||
[排序算法之桶排序的深入理解以及性能分析](https://dailc.github.io/2016/12/03/baseKnowlenge_algorithm_sort_bucketSort.html) |
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,15 @@ | ||
## 原理 | ||
|
||
计数排序的核心在于将输入的数据值转化为键存储在额外开辟的数组空间中。作为一种线性时间复杂度的排序,计数排序要求输入的数据必须是有确定范围的整数。 | ||
|
||
## 算法步骤 | ||
|
||
![countingSort](~@images/src/algorithms/sort/countingSort/images/countingSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/countingSort/index.ts | ||
|
||
## 参考 | ||
|
||
[countingSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/8.countingSort.md) |
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,29 @@ | ||
## 原理 | ||
|
||
堆排序(`Heap sort`)是指利用堆这种数据结构所设计的一种排序算法。堆积是一个近似完全二叉树的结构, 并同时满足堆积的性质: 即子结点的键值或索引总是小于(或者大于)它的父节点。堆排序可以说是一种利用堆的概念来排序的选择排序。分为两种方法: | ||
|
||
- 大顶堆: 每个节点的值都大于或等于其子节点的值, 在堆排序算法中用于升序排列; | ||
|
||
- 小顶堆: 每个节点的值都小于或等于其子节点的值, 在堆排序算法中用于降序排列; | ||
|
||
堆排序的平均时间复杂度为 `Ο(nlogn)`。 | ||
|
||
## 算法步骤 | ||
|
||
- 创建一个堆 H[0……n-1]; | ||
|
||
- 把堆首(最大值)和堆尾互换; | ||
|
||
- 把堆的尺寸缩小 1, 并调用 shift_down(0), 目的是把新的数组顶端数据调整到相应位置; | ||
|
||
- 重复步骤 2, 直到堆的尺寸为 1。 | ||
|
||
![heapSort](~@images/src/algorithms/sort/heapSort/images/heapSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/heapSort/index.ts | ||
|
||
## 参考 | ||
|
||
[heapSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/7.heapSort.md) |
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,24 @@ | ||
## 原理 | ||
|
||
插入排序(Insertion-Sort)的算法描述是一种简单直观的排序算法。它的工作原理是通过构建有序序列, 对于未排序数据, 在已排序序列中从后向前扫描, 找到相应位置并插入。 | ||
|
||
## 算法步骤 | ||
|
||
`n` 个记录的直接选择排序可经过 `n-1` 趟直接选择排序得到有序结果。具体算法描述如下: | ||
|
||
- 从第一个元素开始, 该元素可以认为已经被排序; | ||
- 取出下一个元素, 在已经排序的元素序列中从后向前扫描; | ||
- 如果该元素(已排序)大于新元素, 将该元素移到下一位置; | ||
- 重复步骤`3`, 直到找到已排序的元素小于或者等于新元素的位置; | ||
- 将新元素插入到该位置后; | ||
- 重复步骤 `2~5`。 | ||
|
||
![insertionSort](~@images/src/algorithms/sort/insertionSort/images/insertionSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/insertionSort/index.ts | ||
|
||
## 参考 | ||
|
||
[insertionSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/3.insertionSort.md) |
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,31 @@ | ||
## 原理 | ||
|
||
归并排序(`Merge sort`)是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(`Divide and Conquer`)的一个非常典型的应用。 | ||
|
||
作为一种典型的分而治之思想的算法应用, 归并排序的实现由两种方法: | ||
|
||
- 自上而下的递归 | ||
|
||
- 自下而上的迭代 | ||
|
||
## 算法步骤 | ||
|
||
- 申请空间, 使其大小为两个已经排序序列之和, 该空间用来存放合并后的序列; | ||
|
||
- 设定两个指针, 最初位置分别为两个已经排序序列的起始位置; | ||
|
||
- 比较两个指针所指向的元素, 选择相对小的元素放入到合并空间, 并移动指针到下一位置; | ||
|
||
- 重复步骤 3 直到某一指针达到序列尾; | ||
|
||
- 将另一序列剩下的所有元素直接复制到合并序列尾。 | ||
|
||
![mergeSort](~@images/src/algorithms/sort/mergeSort/images/mergeSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/mergeSort/index.ts | ||
|
||
## 参考 | ||
|
||
[mergeSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/5.mergeSort.md) |
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,29 @@ | ||
## 原理 | ||
|
||
快速排序是由东尼·霍尔所发展的一种排序算法。在平均状况下, 排序 `n`个项目要 `Ο(nlogn)` 次比较。在最坏状况下则需要 `Ο(n2)` 次比较, 但这种状况并不常见。事实上, 快速排序通常明显比其他 `Ο(nlogn)` 算法更快, 因为它的内部循环(`inner loop`)可以在大部分的架构上很有效率地被实现出来。 | ||
|
||
快速排序使用分治法(`Divide and conquer`)策略来把一个串行(`list`)分为两个子串行(`sub-lists`)。 | ||
|
||
快速排序又是一种分而治之思想在排序算法上的典型应用。本质上来看, 快速排序应该算是在冒泡排序基础上的递归分治法。 | ||
|
||
快速排序的最坏运行情况是 `O(n²)`, 比如说顺序数列的快排。但它的平摊期望时间是 `O(nlogn)`, 且 `O(nlogn)` 记号中隐含的常数因子很小, 比复杂度稳定等于 `O(nlogn)` 的归并排序要小很多。所以, 对绝大多数顺序性较弱的随机数列而言, 快速排序总是优于归并排序。 | ||
|
||
## 算法步骤 | ||
|
||
- 从数列中挑出一个元素, 称为'基准' (`pivot`); | ||
|
||
- 重新排序数列, 所有元素比基准值小的摆放在基准前面, 所有元素比基准值大的摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后, 该基准就处于数列的中间位置。这个称为分区(`partition`)操作; | ||
|
||
- 递归地(`recursive`)把小于基准值元素的子数列和大于基准值元素的子数列排序; | ||
|
||
递归的最底部情形, 是数列的大小是零或一, 也就是永远都已经被排序好了。虽然一直递归下去, 但是这个算法总会退出, 因为在每次的迭代(`iteration`)中, 它至少会把一个元素摆到它最后的位置去。 | ||
|
||
![quickSort](~@images/src/algorithms/sort/quickSort/images/quickSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/quickSort/index.ts | ||
|
||
## 参考 | ||
|
||
[quickSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/6.quickSort.md) |
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,23 @@ | ||
## 原理 | ||
|
||
基数排序是一种非比较型整数排序算法, 其原理是将整数按位数切割成不同的数字, 然后按每个位数分别比较。由于整数也可以表达字符串(比如名字或日期)和特定格式的浮点数, 所以基数排序也不是只能使用于整数。 | ||
|
||
## 算法步骤 | ||
|
||
基数排序有两种方法: | ||
|
||
这三种排序算法都利用了桶的概念, 但对桶的使用方法上有明显差异: | ||
|
||
- 基数排序: 根据键值的每位数字来分配桶; | ||
- 计数排序: 每个桶只存储单一键值; | ||
- 桶排序: 每个桶存储一定范围的数值; | ||
|
||
![radixSort](~@images/src/algorithms/sort/radixSort/images/radixSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/radixSort/index.ts | ||
|
||
## 参考 | ||
|
||
[radixSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/10.radixSort.md) |
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,21 @@ | ||
## 原理 | ||
|
||
选择排序是一种简单直观的排序算法 `d`, 无论什么数据进去都是 `O(n²)` 的时间复杂度。所以用到它的时候`d`, 数据规模越小越好。唯一的好处可能就是不占用额外的内存空间了吧。 | ||
|
||
## 算法步骤 | ||
|
||
- 首先在未排序序列中找到最小(大)元素 `d`, 存放到排序序列的起始位置; | ||
|
||
- 再从剩余未排序元素中继续寻找最小(大)元素 `d`, 然后放到已排序序列的末尾; | ||
|
||
- 重复第二步d, 直到所有元素均排序完毕。 | ||
|
||
![selectionSort](~@images/src/algorithms/sort/selectionSort/images/selectionSort.gif) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/selectionSort/index.ts | ||
|
||
## 参考 | ||
|
||
[selectionSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/2.selectionSort.md) |
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,32 @@ | ||
## 原理 | ||
|
||
希尔排序, 也称递减增量排序算法, 是插入排序的一种更高效的改进版本。但希尔排序是非稳定排序算法。 | ||
|
||
希尔排序是基于插入排序的以下两点性质而提出改进方法的: | ||
|
||
- 插入排序在对几乎已经排好序的数据操作时, 效率高, 即可以达到线性排序的效率; | ||
|
||
- 但插入排序一般来说是低效的, 因为插入排序每次只能将数据移动一位; | ||
希尔排序的基本思想是:先将整个待排序的记录序列分割成为若干子序列分别进行直接插入排序, 待整个序列中的记录'基本有序'时, 再对全体记录进行依次直接插入排序。 | ||
|
||
|
||
|
||
## 算法步骤 | ||
|
||
- 选择一个增量序列 t1, t2, ……, tk, 其中 ti > tj, tk = 1; | ||
|
||
- 按增量序列个数 k, 对序列进行 k 趟排序; | ||
|
||
- 每趟排序, 根据对应的增量 ti, 将待排序列分割成若干长度为 m 的子序列, 分别对各子表进行直接插入排序。仅增量因子为 1 时, 整个序列作为一个表来处理, 表长度即为整个序列的长度。 | ||
|
||
![shellSort](~@images/src/algorithms/sort/shellSort//images/shellSort.gif) | ||
|
||
![shellSort](~@images/src/algorithms/sort/shellSort/images/shellSort.png) | ||
|
||
## 实现代码 | ||
|
||
<<< @/src/algorithms/sort/shellSort/index.ts | ||
|
||
## 参考 | ||
|
||
[shellSort](https://github.com/Rain120/JS-Sorting-Algorithm/blob/master/4.shellSort.md) |