mirror of
https://github.com/chefyuan/algorithm-base.git
synced 2025-08-05 15:12:22 +00:00
代码重构 【Github Actions】
This commit is contained in:
@@ -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:
|
||||
|
||||

|
||||
|
||||
|
||||
|
||||
比如此时小集合大小为 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]
|
||||
```
|
||||
|
Reference in New Issue
Block a user