当前位置:网站首页>排序——归并排序
排序——归并排序
2022-06-11 07:37:00 【AcTarjan】
归并排序
算法设计
- 将大的问题分解成多个子问题进行求解,子问题还可以继续分解,直到问题规模可以直接求解。
- 将序列分成两个子序列,使子序列有序,然后将两个有序的子序列合并成有序序列
C语言实现
void mergeSort(int* arr,int start,int end) {
int len = end - start;
if (len <= 1) {
return;
}
int mid = (start+end)/2;
mergeSort(arr,start,mid);
mergeSort(arr,mid,end);
int* temp = (int*)malloc(len * sizeof(int));
int l=start,r=mid,index=0;
while (l< mid && r < end) {
if (arr[l] <= arr[r]) {
temp[index] = arr[l];
l++;index++;
} else {
temp[index] = arr[r];
r++;index++;
}
}
while (l < mid) {
temp[index] = arr[l];
l++;index++;
}
while (r < end) {
temp[index] = arr[r];
r++;index++;
}
for (l = start,index = 0; l < end; l++,index++) {
arr[l] = temp[index];
}
free(temp);
}
算法分析
- 稳定的
- 空间复杂度为O(n)
- 当两个子序列逐个交替输出时,比较次数最大;当其中一个子序列直接输出时,比较次数最小。但总的时间复杂度依然为O(nlogn)
- 平均时间复杂度为O(nlogn)
边栏推荐
猜你喜欢

Building a full-featured NAS server with raspberry pie (05): playing with video and audio & sorting out movies

零基础自学SQL课程 | UNION 联合查询

2022.5.30-6.5 AI行业周刊(第100期):三年时光

Seata的几种事务模式

2022 low voltage electrician operation certificate test question simulation test platform operation

Post-processing of ffmpeg miscellaneous notes

【AtCoder1980】Mysterious Light(数学模拟)
![20200727 T2 small w playing game [generating function (binomial inversion technique)]](/img/a5/ae2192f4f37232cdcb01e81ad0297c.jpg)
20200727 T2 small w playing game [generating function (binomial inversion technique)]

黑群晖DSM7.0.1物理机安装教程

2022低压电工操作证考试题模拟考试平台操作
随机推荐
QT table display data
Uoj 551 [unr 4] campus stroll [good polynomial questions (FOG)]
Post-processing of ffmpeg miscellaneous notes
QObject usage skills -- control function class
Flask页面的分页
gaussDB for redis和 redis的区别?
【HDU6357】Hills And Valleys(DP)
【POJ3691】DNA repair (AC自动机+DP)
软件测试周刊(第75期):唯有平视,才能看见真实的自己。
[atcoder1998] stamp Rally
Nim product
Wc2020 course selection
【CodeForces1019E】Raining season(边分治+斜率优化)
Summary of written test questions of shopee 2021 autumn recruitment
Seata的几种事务模式
二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
Compound ratemodel contract analysis
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
A case in which the MySQL administrator password cannot take effect
Classification of MNIST datasets with keras