当前位置:网站首页>归并排序
归并排序
2022-07-21 05:15:00 【龙星尘】
上期我跟大家讲了快速排序,复杂度非常的不稳定,归并也是一个广泛运用的排序算法,为什么呢?
- 稳定,快速
- 递归调用,分治思想
- 合并两个有序数组的思想
- 时间复杂度O(nlog2n),这个时间复杂度的分析方法很重要一定要掌握
归并排序非常的稳定,时间复杂度不论是平均,还是最好,还是最坏,复杂度都是O(nlog2n),几乎是一成不变的。
如上图,列下来了我跟大家讲的排序算法的各个情况下的时间复杂度和空间复杂度以及稳定性。
接下来我正式讲解归并排序:
归并排序是建立在归并操作上的一种有效的排序算法。该算法是采用分治法(Divide and Conquer)的一个非常典型的应用。将已有序的子序列合并,得到完全有序的序列;即先使每个子序列有序,再使子序列段间有序。若将两个有序表合并成一个有序表,称为2-路归并。
可以看到这种结构很像一棵完全二叉树,本文的归并排序我们采用递归去实现(也可采用迭代的方式去实现)。分阶段可以理解为就是递归拆分子序列的过程,递归深度为log2n。
再来看看治阶段,我们需要将两个已经有序的子序列合并成一个有序序列,比如上图中的最后一次合并,要将[4,5,7,8]和[1,2,3,6]两个已经有序的子序列,合并为最终序列[1,2,3,4,5,6,7,8],来看下实现步骤。
算法描述
- 把长度为n的输入序列分成两个长度为n/2的子序列;
- 对这两个子序列分别采用归并排序;
- 将两个排序好的子序列合并成一个最终的排序序列。
动画演示:
归并排序代码:
void ef(int v[],int c,int m, int d,int tmp[])
{
int pb=0;
int p1=c,p2=m+1;
while(p1<=m&&p2<=d)
{
if(v[p1]<v[p2])
tmp[pb++]=v[p1++];
else
tmp[pb++]=v[p2++];
}
while(p1<=m)
tmp[pb++]=v[p1++];
while(p2<=d)
tmp[pb++]=v[p2++];
for(int i=0;i<d-c+1;++i)
v[c+i]=tmp[i];
}
void hbpx(int n[],int s,int e,int tmq[])
{
if(s<e)
{
int m=s+(e-s)/2;
hbpx(n,s,m,tmq);
hbpx(n,m+1,e,tmq);
ef(n,s,m,e,tmq);
}
}
在这里我希望大家可以熟练运用归并排序,再见。
边栏推荐
- Interviewer: have you made a judgment on the number of rows affected by your update?
- 3级学业水平测试
- 超级实用的12条 SQL 优化方案
- 12 solutions d'optimisation SQL super pratiques
- Advanced visual studio features
- Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
- 04 unittest unit test framework
- 04-unittest單元測試框架
- 堆-原理到应用——堆排序、优先级队列
- RobotFramework(ride)关键字无法使用或关键字为黑色
猜你喜欢
Heap - principle to application - heap sort, priority queue
Yolov4 (Darknet version) post processing: display confidence and save the contents of the detection box locally
Principles and advantages of wireless charging module development
Super practical 12 SQL optimization schemes
2020普及组总结
面试题:聚簇索引和非聚簇索引有什么区别?
205. 同构字符串
干货 | 分布式系统的数据分片难题
Appium+pytest+allure realizes app automated testing, which is a small test
Teach you how to use Charles to grab bags
随机推荐
Pytorch2onnx2Tensorflow的环境准备win10
Concurrency opening -- take you from 0 to 1 to build the cornerstone of concurrency knowledge system
robotframework 常用快捷键
567. 字符串的排列
面试题:聚簇索引和非聚簇索引有什么区别?
Learning notes for beginners to OpenGL (II) Download glew, configure the environment of glew and initialization of glew
Common shortcut keys of robotframework
Heap - principle to application - heap sorting, priority queue
Summary of Niuke online question brushing -- top101 must be brushed for interview
企业如何做好数据管理?产品选型怎么做?
Do you really understand the 01 backpack problem? Can you answer these questions of 01 backpack?
01背包面试题系列(一)
Do you think sub database and sub table are really suitable for your system? Talk about how to select sub databases, sub tables and newsql
03-selenium的自动化测试模型
22张图带你深入剖析前缀、中缀、后缀表达式以及表达式求值
beta.4 版发布啦,国产开源数据可视化 datart 接下来将会小跑进入 rc 阶段
148. 排序链表
Vs2019+opencv installation and configuration tutorial
It turns out that someone plays vscode into idea, which is a little powerful
robotframework--浏览器驱动和操作的实现(1)