当前位置:网站首页>归并排序
归并排序
2022-06-29 02:46:00 【疯疯癫癫才自由】
2——路归并排序,将序列两两分组进行排序,共有n/2组;然后再将这些组两两进行合并排序,得到n/4组,以此类推,直到只剩下一个组为止,此时这个组便是有序的。
两两分组的时候,可以用基于二分的思想进行分组。
void mergesort(int *a,int l,int r)
{
if(l>=r)
return;
else
{
int mid=l+(r-l)/2;
mergesort(a,l,mid);
mergesort(a,mid+1,r);
my_merge(a,l,mid,mid+1,r);
}
}
归并排序
void my_merge(int *a,int l1,int r1,int l2,int r2)//两个有序序列,合并为一个有序序列,tow point :将两个有序序列合并为一个有序序列,O(n)
void my_merge(int *a,int l1,int r1,int l2,int r2)//两个有序序列,合并为一个有序序列
{
int temp[r2-l1+1],index=0,i=l1,j=l2;
while(i<=r1&&j<=r2)
{
if(a[i]<a[j])
temp[index++]=a[i++];
else
temp[index++]=a[j++];
}while(i<=r1)
temp[index++]=a[i++];
while(j<=r2)
temp[index++]=a[j++];
for(i=0;i<index;++i)
a[l1+i]=temp[i];
}
/**
归并排序
tow point :将两个有序序列合并为一个有序序列,O(n)
*/
/**
#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std;
void my_merge(int *a,int l1,int r1,int l2,int r2)//两个有序序列,合并为一个有序序列
{
int temp[r2-l1+1],index=0,i=l1,j=l2;
while(i<=r1&&j<=r2)
{
if(a[i]<a[j])
temp[index++]=a[i++];
else
temp[index++]=a[j++];
}
while(i<=r1)
temp[index++]=a[i++];
while(j<=r2)
temp[index++]=a[j++];
for(i=0;i<index;++i)
a[l1+i]=temp[i];
}
void mergesort(int *a,int l,int r)
{
if(l>=r)
return;
else
{
int mid=l+(r-l)/2;
mergesort(a,l,mid);
mergesort(a,mid+1,r);
my_merge(a,l,mid,mid+1,r);
}
}
int main()
{
int n;
cin >> n;
int a[n];
for(int i=0;i<n;++i)
cin >> a[i];
mergesort(a,0,n);
for(auto b: a)
cout << b << ' ';
cout << endl;
cout << "Hello world!" << endl;
return 0;
}边栏推荐
- 【一起上水硕系列】Day 6-强肝学术论文!最细解释!
- [linear algebra] 1.1 second and third order determinants
- 18. `bs object Node name next_ Sibling` get sibling nodes
- 99乘法表
- Mipi d-phy -- contents of HS and LP agreements
- Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter
- 双击事件与单击事件的那些事
- String method exercise
- 1110: nearest common ancestor (function topic)
- 深入解析 Apache BookKeeper 系列:第三篇——读取原理
猜你喜欢

Overview of PMP project management

mgalcu-a509

Oracle Recovery Tools实战批量坏块修复

thinkphp5.1 runtime文件改成777权限了, 还是无法写入

微信小程序自定义组件

Have you learned the common SQL interview questions on the short video platform?

微信小程序安全登录,必须先校验微信返回openid,确保信息唯一性。

Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)

Learning Tai Chi Maker - mqtt Chapter II (IX) test of this chapter

Talk about the copyonwritearraylist of JUC
随机推荐
mark
Introduction to openresty
How does sound amplify weak sounds
Informatics Olympiad all in one 1361: production | Luogu P1037 [noip2002 popularization group] production
信息学奥赛一本通 1361:产生数(Produce) | 洛谷 P1037 [NOIP2002 普及组] 产生数
Trigonometric function calculation
Square root of X
thinkphp5.1 runtime文件改成777权限了, 还是无法写入
[linear algebra] 1.1 second and third order determinants
认证培训|StreamNative Certification 培训第2期
PHP system function
今日直播|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭
HashSet storing objects and how to not store the same objects
Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)
对补wasm环境的一些测试
50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
[untitled]
Has Moore's law come to an end?
Talk about the copyonwritearraylist of JUC
解决allegro中测量距离时,点击一个点后光标闪烁的问题