当前位置:网站首页>排序——归并排序
排序——归并排序
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)
边栏推荐
- 【CodeForces1019E】Raining season(边分治+斜率优化)
- Bidirectional linked list simple template (pointer version)
- [IOT] project management: how to build a better cross functional team?
- Sdl-4 PCM playback
- [Oracle database] mammy tutorial day04 Sorting Query
- 【集群】haproxy负载均衡
- Tetris preliminary
- [codeforces1019e] raining season
- Classes and objects (Part 2)
- 2020080 simulation competition [horizontal and vertical coordinates do not affect each other, cactus minimum cut, combined meaning translation formula]
猜你喜欢

二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...

If you want to save an IP address, what data type is better? 99% of people will answer wrong!

二本畢業,銀行外包測試工作 4 個月有餘。聊聊一些真實感受 ...

【Oracle 数据库】奶妈式教程day04 排序查询

Qstring to hexadecimal qstring

JVM tuning

Use of wordcloud

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

May 30-June 5, 2022 AI industry weekly (issue 100): three years

C language inherits memory management mechanism (unfinished)
随机推荐
C language three chess games
【编译原理】05-语法制导的语义计算——基于翻译模式的语义计算
【AtCoder1981】Shorten Diameter(图论思维)
Tetris preliminary
Ffmpeg extraction package format extracts AAC and customizes adtc header to realize arbitrary frame decoding
C language to write a calculator calculation logic
【CodeForces908H】New Year and Boolean Bridges (FWT)
20200727 T2 small w playing game [generating function (binomial inversion technique)]
[STL source code analysis] summary notes (5): a good helper for understanding iterators --list
C language to achieve minesweeping games
Post-processing of ffmpeg miscellaneous notes
[cluster] haproxy load balancing
Second remaining learning notes
Compound RateModel合约解析
[atcoder1980] mystious light (mathematical simulation)
[STL source code analysis] summary note (2): overview of containers
C language function stack frame
Paging of the flask page
SQLZOO刷题记录-3
[analysis of STL source code] summary notes (3): vector introduction