当前位置:网站首页>排序——归并排序
排序——归并排序
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(边分治+斜率优化)
- [STL source code analysis] summary notes (1): Opening
- Summary of classic interview questions
- Tetris preliminary
- 3年功能测试拿8K,被新来的反超,其实你在假装努力
- Zero foundation self-study SQL course | outer join external connection
- I/o multiplexing - select/poll/epoll
- C language to write a calculator calculation logic
- 【HDU6357】Hills And Valleys(DP)
- 【软件测试】这样的简历已经刷掉了90%的面试者
猜你喜欢

Deux diplômés, la Banque a externalisé le travail d'essai pendant plus de quatre mois. Parler de vrais sentiments...

【AtCoder2306】Rearranging(拓扑)

测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...
![[STL source code analysis] summary note (2): overview of containers](/img/66/60fba564ae6020dfb503c7fdf78529.jpg)
[STL source code analysis] summary note (2): overview of containers

C language to write a calculator calculation logic

Post-processing of ffmpeg miscellaneous notes

Several transaction modes of Seata

Sdl-2 thread logic

JVM tuning

C language function stack frame
随机推荐
Uoj 551 [unr 4] campus stroll [good polynomial questions (FOG)]
10 advanced concepts that must be understood in learning SQL
2、 User login and registration
Building a full-featured NAS server with raspberry pie (05): playing with video and audio & sorting out movies
JVM tuning
[STL source code analysis] summary notes (9): set/multiset and map/multimap
What is the lifecycle of automated testing?
Configuration software -- control import
Nim product
欧拉定理及扩展(附证明)
2022低压电工操作证考试题模拟考试平台操作
Arduino_ Esp32 development record
QObject usage skills -- control function class
C language three chess games
2022 low voltage electrician test questions and online simulation test
3年功能测试拿8K,被新来的反超,其实你在假装努力
[IOT] project management: how to build a better cross functional team?
Aiop introduction
A correction book full of sad tears
[codeforces1019e] raining season