当前位置:网站首页>C language implements eight sorts of sort merge sort
C language implements eight sorts of sort merge sort
2022-06-11 21:58:00 【Code loving students】
What is merging order ?
Merge sort It is an effective sorting algorithm based on merge operation , The algorithm adopts Divide and conquer method A very typical application .
Merges ordered subsequences , You get a perfectly ordered sequence ; So let's make each subsequence in order , Then make the subsequence segments in order . If two ordered tables are merged into one ordered table , It's called a two-way merge .
The steps are shown in the figure :

So how can we achieve it ?
1. Recursive method
The code is as follows :
// Merge sort
// Merge two ordered arrays
void Sort(int*a,int left,int mid,int right){
int*p=(int*)malloc(sizeof(int)*(right-left+1)); // Opening up a size is right-left+1 Space
int p1=left;
int p2=mid+1;
int p3=0;
while(p1<=mid&&p2<=right){
if(a[p1]<a[p2]){
p[p3++]=a[p1++];
}
else{
p[p3++]=a[p2++];
}
}
while(p1<=mid)
p[p3++]=a[p1++];
while(p2<=right)
p[p3++]=a[p2++];
for(int j=0;j<p3;j++){
a[left+j]=p[j];
}
free(p);
}
void MergeSort(int*a,int left,int right){
int mid;
if(left>=right){ // When an array has only one element , Can be seen as orderly
return;
}
mid=(left+right)/2;
MergeSort(a,left,mid); // Sort left half array
MergeSort(a,mid+1,right); // Sort the right half of the array
Sort(a,left,mid,right); // Merge two ordered arrays
} In this way, we have completed the recursive version of merge sorting
2. Non recursive version
As we can see from the picture , Finally, the first to merge is the array with only one element , Then we can use non recursion to realize the last step first :
// Non recursive writing
void MergeSort2(int*a,int n){
int*tmp = (int*)malloc(sizeof(int)*n);
int gap=1;// Similar to the last step of recursion directly first
while(gap<n){
for(int i = 0 ; i < n; i += 2*gap){
int start1=i,end1=i+gap-1; // The first interval
int start2=gap+i,end2=2*gap+i-1; // The second interval
int index=i;// Each initialization position is the starting position of the first interval
while(start1<=end1&&start2<=end2){
if(a[start1]<a[start2])
tmp[index++]=a[start1++];
else
tmp[index++]=a[start2++];
}
while(start1<=end1)
tmp[index++]=a[start1++];
while(start2<=end2)
tmp[index++]=a[start2++];
}
memcpy(a,tmp,n*sizeof(int));
gap *= 2;
}
free(tmp);
} We will start with the size of the array by 1 Start , Merge in pairs , Finally complete the recursive operation , Is that the end ?
Let's look at each merged array :

What we have here is just 9 An array of element sizes , Why did you cross the border so many times .

Then how should we modify it , Make it impossible to cross the border ?
Here we need to readjust our range :
int start1=i,end1=i+gap-1; // The first interval
int start2=gap+i,end2=2*gap+i-1; // The second interval
This is the interval we defined before , From the above figure, we can also see that in addition to start1 He's a good boy and doesn't cross the line , The others have crossed the line , Then we should criticize and correct them .
Here we need to consider three situations :
1. If end1 Transboundary
2. If end2 Transboundary
3. If start2 Transboundary
The modification operation is as follows :
if(end1>=n){
end1=n-1;
}
if(start2>=n){
start2=n;
end2=n-1;
}
if(start2<n&&end2>=n){
end2=n-1;
}In this way, we can well realize our operation
边栏推荐
- Daily question - Roman numeral to integer
- servlet获取表单数据
- Go OS module
- Flink error: multiple tasks are started, and only one task is executed
- 206. reverse linked list
- Carry and walk with you. Have you ever seen a "palm sized" weather station?
- LaTex实战笔记 3-宏包与控制命令
- [academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
- 高考结束,人生才刚刚开始,10年职场老鸟给的建议
- 相对完善的单例模式
猜你喜欢

类和对象(4)

The shortcomings of the "big model" and the strengths of the "knowledge map"

RPA super automation | nongnongji and cloud expansion accelerate financial intelligent operation

快速排序的优化

Leetcode-155-minimum stack

【历史上的今天】6 月 11 日:蒙特卡罗方法的共同发明者出生;谷歌推出 Google 地球;谷歌收购 Waze

Introduction à endnotex9 et instructions pour les tutoriels de base

网络连接正常但百度网页打不开显示无法访问此网站解决方案

Top - k问题

每日一题 -- 验证回文串
随机推荐
C语言实现八种排序(3)
Leetcode-110-balanced binary tree
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
RPA+低代码助推品牌电商启新创变、重启增长
8、 BOM - chapter after class exercises and answers
JVM class loader; Parental delegation mechanism
Custom implementation offsetof
华为设备配置H-VPN
How to use the transaction code sat to find the name trial version of the background storage database table corresponding to a sapgui screen field
go io模块
类和对象(4)
Top - k问题
win10字体模糊怎么调节
Leetcode-322- change exchange
类和对象(2)
Matlab: 文件夹锁定问题的解决
华为设备配置HoVPN
Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.3
Win10弹出USB时出现该设备正在使用的解决方法
The upcoming launch of the industry's first retail digital innovation white paper unlocks the secret of full link digital success