代码重构 【Github Actions】

This commit is contained in:
github-actions[bot]
2021-07-23 15:44:19 +00:00
parent c79cac3d9c
commit f671c90754
94 changed files with 1609 additions and 2111 deletions

View File

@@ -1,10 +1,10 @@
### **归并排序**
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
> 如果阅读时发现错误或者动画不可以显示的问题可以添加我微信好友 **[tan45du_one](https://raw.githubusercontent.com/tan45du/tan45du.github.io/master/个人微信.15egrcgqd94w.jpg)** ,备注 github + 题目 + 问题 向我反馈
>
> 感谢支持该仓库会一直维护希望对各位有一丢丢帮助
>
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
> 另外希望手机阅读的同学可以来我的 <u>[**公众号袁厨的算法小屋**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u> 两个平台同步,想要和题友一起刷题,互相监督的同学,可以在我的小屋点击<u>[**刷题小队**](https://raw.githubusercontent.com/tan45du/test/master/微信图片_20210320152235.2pthdebvh1c0.png)</u>进入。
归并排序是必须要掌握的排序算法也算是面试高频考点下面我们就一起来扒一扒归并排序吧原理很简单大家一下就能搞懂
@@ -12,13 +12,13 @@
23 届食神争霸赛开赛啦
袁厨想在自己排名前4的分店中挑选一个最优秀的厨师来参加食神争霸赛选拔规则如下
袁厨想在自己排名前 4 的分店中挑选一个最优秀的厨师来参加食神争霸赛选拔规则如下
第一场 PK每个分店选出两名厨师首先进行店内 PK,选出店内里的胜者
第二场 PK: 然后店内的优胜者代表分店挑战其他某一分店的胜者半决赛
第二场 PK: 然后店内的优胜者代表分店挑战其他某一分店的胜者半决赛
第三场 PK最后剩下的两名胜者进行PK,选出最后的胜者
第三场 PK最后剩下的两名胜者进行 PK,选出最后的胜者
示意图如下
@@ -69,7 +69,7 @@ class Solution {
mergeSort(arr,mid+1,right);
merge(arr,left,mid,right);
}
}
}
//归并
public void merge(int[] arr,int left, int mid, int right) {
//第一步,定义一个新的临时数组
@@ -87,7 +87,7 @@ class Solution {
//对应第三步,将某一小集合的剩余元素存到大集合中
if (temp1 <= mid) System.arraycopy(arr, temp1, temparr, index, mid - temp1 + 1);
if (temp2 <= right) System.arraycopy(arr, temp2, temparr, index, right -temp2 + 1); //将大集合的元素复制回原数组
System.arraycopy(temparr,0,arr,0+left,right-left+1);
System.arraycopy(temparr,0,arr,0+left,right-left+1);
}
}
```
@@ -106,8 +106,8 @@ class Solution:
mid = left + ((right - left) >> 1)
self.mergeSort(arr, left, mid)
self.mergeSort(arr, mid + 1, right)
self.merge(arr, left, mid, right)
self.merge(arr, left, mid, right)
# 归并
def merge(self, arr: List[int], left: int, mid: int, right: int):
# 第一步定义一个新的临时数组
@@ -128,15 +128,16 @@ class Solution:
# 对应第三步将某一集合的剩余元素存到大集合中
if temp1 <= mid:
temparr[index: index + mid - temp1 + 1] = arr[temp1: temp1 + mid - temp1 + 1]
if temp2 <= right:
if temp2 <= right:
temparr[index: index + right - temp2 + 1] = arr[temp2: temp2 + right - temp2 + 1]
# 将大集合的元素复制回原数组
arr[left: left + right- left + 1] = temparr[0: right - left + 1]
```
**归并排序时间复杂度分析**
我们一趟归并需要将两个小集合的长度放到大集合中则需要将待排序序列中的所有记录扫描一遍所以时间复杂度为O(n)归并排序把集合一层一层的折半分组则由完全二叉树的深度可知整个排序过程需要进行 logn向上取整,则总的时间复杂度为 O(nlogn)另外归并排序的执行效率与要排序的原始数组的有序程度无关所以在最好最坏平均情况下时间复杂度均为 O(nlogn) 虽然归并排序时间复杂度很稳定但是他的应用范围却不如快速排序广泛这是因为归并排序不是原地排序算法空间复杂度不为 O(1)那么他的空间复杂度为多少呢
我们一趟归并需要将两个小集合的长度放到大集合中则需要将待排序序列中的所有记录扫描一遍所以时间复杂度为 O(n)归并排序把集合一层一层的折半分组则由完全二叉树的深度可知整个排序过程需要进行 logn向上取整,则总的时间复杂度为 O(nlogn)另外归并排序的执行效率与要排序的原始数组的有序程度无关所以在最好最坏平均情况下时间复杂度均为 O(nlogn) 虽然归并排序时间复杂度很稳定但是他的应用范围却不如快速排序广泛这是因为归并排序不是原地排序算法空间复杂度不为 O(1)那么他的空间复杂度为多少呢
**归并排序的空间复杂度分析**
@@ -144,7 +145,7 @@ class Solution:
**归并排序的稳定性分析**
归并排序的稳定性要看我们的 merge 函数我们代码中设置了 arr[temp1] <= arr[temp2] 当两个元素相同时先放入arr[temp1] 的值到大集合中所以两个相同元素的相对位置没有发生改变所以归并排序是稳定的排序算法
归并排序的稳定性要看我们的 merge 函数我们代码中设置了 arr[temp1] <= arr[temp2] 当两个元素相同时先放入 arr[temp1] 的值到大集合中所以两个相同元素的相对位置没有发生改变所以归并排序是稳定的排序算法
| 算法名称 | 最好时间复杂度 | 最坏时间复杂度 | 平均时间复杂度 | 空间复杂度 | 是否稳定 |
| -------- | -------------- | -------------- | -------------- | ---------- | -------- |
@@ -164,8 +165,6 @@ class Solution:
![](https://cdn.jsdelivr.net/gh/tan45du/bedphoto2@master/20210122/微信截图_20210203205336.4j443ciyj7u0.png)
比如此时小集合大小为 1 两个小集合分别为 [3],[1]然后我们根据合并规则见第一个视频[3],[1]合并到临时数组中则小的先进则实现了排序然后再将临时数组的元素复制到原来数组中则实现了一次合并
下面则继续合并[4],[6]具体步骤一致所有的小集合合并完成后则小集合的大小变为 2继续执行刚才步骤见下图
@@ -176,7 +175,7 @@ class Solution:
下面我们直接看代码吧
递归法和迭代法的 merge函数代码一样
递归法和迭代法的 merge 函数代码一样
Java Code:
@@ -221,9 +220,9 @@ class Solution {
}
//对应第三步,将某一小集合的剩余元素存到大集合中
if (temp1 <= mid) System.arraycopy(arr, temp1, temparr, index, mid - temp1 + 1);
if (temp2 <= right) System.arraycopy(arr, temp2, temparr, index, right -temp2 + 1);
if (temp2 <= right) System.arraycopy(arr, temp2, temparr, index, right -temp2 + 1);
//将大集合的元素复制回原数组
System.arraycopy(temparr,0,arr,0+left,right-left+1);
System.arraycopy(temparr,0,arr,0+left,right-left+1);
}
}
```
@@ -249,7 +248,7 @@ class Solution:
# 归并
self.merge(array, i, i + k - 1, i + 2 * k - 1)
i += 2 * k
# 归并最后两个序列
if i + k < leng:
self.merge(array, i, i + k - 1, leng - 1)
@@ -274,9 +273,9 @@ class Solution:
# 对应第三步将某一集合的剩余元素存到大集合中
if temp1 <= mid:
temparr[index: index + mid - temp1 + 1] = arr[temp1: temp1 + mid - temp1 + 1]
if temp2 <= right:
if temp2 <= right:
temparr[index: index + right - temp2 + 1] = arr[temp2: temp2 + right - temp2 + 1]
# 将大集合的元素复制回原数组
arr[left: left + right- left + 1] = temparr[0: right - left + 1]
```