当前位置:网站首页>Sort - merge sort
Sort - merge sort
2022-06-11 07:40:00 【AcTarjan】
Merge sort
Algorithm design
- Decompose a large problem into several sub problems for solution , The subproblem can be further decomposed , Until the scale of the problem can be solved directly .
- Divide the sequence into two subsequences , To make subsequences ordered , Then two ordered subsequences are merged into an ordered sequence
C Language implementation
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);
}
Algorithm analysis
- The stability of the
- The space complexity is O(n)
- When two subsequences are output alternately one by one , The largest number of comparisons ; When one of the subsequences is output directly , Minimum number of comparisons . But the total time complexity is still O(nlogn)
- The average time complexity is zero O(nlogn)
边栏推荐
- How to prepare for the new PMP syllabus exam?
- 2022 melting welding and thermal cutting exam exercises and answers
- 二本毕业,银行外包测试工作 4 个月有余。聊聊一些真实感受 ...
- Several transaction modes of Seata
- Arduino_ Esp32 development record
- Classes and objects (medium)
- 【AtCoder1984】Wide Swap (拓扑排序转化)
- 【HDU6357】Hills And Valleys(DP)
- Use of wordcloud
- 【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
猜你喜欢

Seata的几种事务模式
![[atcoder2306] rearranging (topology)](/img/b3/38589a07a7c26bea8ed154ab794760.png)
[atcoder2306] rearranging (topology)

【Oracle 数据库】奶妈式教程day04 排序查询
![[STL source code analysis] summary notes (10): hashtable exploration](/img/31/a77ac380dbd0f85957bd1df1b906f5.jpg)
[STL source code analysis] summary notes (10): hashtable exploration

Flask页面的分页

测试4年裸辞失业,面试15k的测试岗被按在地上摩擦,结局让我崩溃大哭...

Qunhui ds918 creates m.2 SSD read / write cache

2022 melting welding and thermal cutting exam exercises and answers

C language inherits memory management mechanism (unfinished)
![[analysis of STL source code] summary note (4): behind the scenes hero allocator](/img/b9/cf53fd8f933042ff65844d61eca55e.jpg)
[analysis of STL source code] summary note (4): behind the scenes hero allocator
随机推荐
Sdl-4 PCM playback
C language Yanghui triangle code
May 30-June 5, 2022 AI industry weekly (issue 100): three years
[analysis of STL source code] summary notes (3): vector introduction
【AtCoder2304】Cleaning
Aiop introduction
nosqlzoo刷题-1
Flask页面的分页
Matrix tree theorem
C memory alignment
[atcoder1981] short diameter (graph theory thinking)
[STL source code analysis] summary note (2): overview of containers
C+tinycthread implementation thread
如果要存 IP 地址,用什么数据类型比较好?99%人都会答错!
Pat class A by category
Software testing weekly (issue 75): only when you look down, can you see your true self.
远程办公经验 | 社区征文
【AtCoder1981】Shorten Diameter(图论思维)
Classification of MNIST datasets with keras
【CodeForces908H】New Year and Boolean Bridges (FWT)