当前位置:网站首页>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)
边栏推荐
- QObject usage skills -- control function class
- Pat class A by category
- 【HDU6357】Hills And Valleys(DP)
- C memory alignment
- Qstring to hexadecimal qstring
- Second remaining learning notes
- I/o multiplexing - select/poll/epoll
- Use of wordcloud
- What is the difference between gaussdb for redis and redis?
- 【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
猜你喜欢

Raspberry pie builds a full-featured NAS server (07): manage your library & read as you please

C memory alignment

C language Yanghui triangle code

Import on CSDN MD file

C language function stack frame
![[software testing] 90% of the interviewers have been brushed out of such resumes](/img/2f/bb4819b98592f750dec92d4b4dd6b7.png)
[software testing] 90% of the interviewers have been brushed out of such resumes

【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用

Miscellany C language

Implementation of stack (C language)

2022 melting welding and thermal cutting exam exercises and answers
随机推荐
模线性方程组(中国剩余定理+通用解法)
【Oracle 数据库】奶妈式教程day02 数据库管理工具SQLPLUS的使用
Uoj 554 [unr 4] challenges Hamilton [find Hamilton path (adjustment method)]
[analysis of STL source code] summary notes (3): vector introduction
【AtCoder2000】Leftmost Ball (DP+组合数)
C language to achieve minesweeping games
Sdl-3 YUV playback
远程办公经验 | 社区征文
如何准备PMP新版大纲考试?
Qstring to hexadecimal qstring
Sqlzoo question brushing record-3
Rabin-Miller素数测试
[cluster] lvs+keepalived high availability cluster
【AtCoder2376】Black and White Tree(博弈)
零基础自学SQL课程 | UNION 联合查询
C language to write a calculator MVC (very interesting code architecture callback and constructor mode and the use of pointer functions)
【AtCoder2307】Tree Game(博弈)
[cluster] haproxy load balancing
[software testing] 90% of the interviewers have been brushed out of such resumes
【CodeForces908H】New Year and Boolean Bridges (FWT)