当前位置:网站首页>归并排序
归并排序
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;
}边栏推荐
- Redis master-slave replication
- Deploy redis high availability cluster
- 【無標題】
- Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
- Download and installation of MySQL
- PMP Business Analysis Overview
- Which securities company is the largest and safest? Which securities company has good service
- 如何在关闭socket连接的时候跳过TIME_WAIT的等待状态
- Bluetooth solution | Lenz technology Amazon direct connected magic lantern solution
- Square root of X
猜你喜欢

音响是如何把微弱声音放大呢

今日直播|Apache Pulsar x KubeSphere 在线 Meetup 火热来袭

学习太极创客 — MQTT 第二章(九)本章测试

"The first share of endoscope" broke into IPO two times. Last year, it lost 500million yuan. The commercialization of core products is still in doubt | IPO Express

Leetcode counts the logarithm of points that cannot reach each other in an undirected graph

Ctfhub web password default password
![[together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!](/img/70/595a94ba19d29a56a4f0bb5964a199.png)
[together with Shangshui Shuo series] day 6-strong liver academic paper! The most detailed explanation!

微信小程序自定义组件

Relations EMC, EMI, EMS

Sysbench Pressure Test Oracle (installation and use examples)
随机推荐
双击事件与单击事件的那些事
Leetcode counts the logarithm of points that cannot reach each other in an undirected graph
Only in the past four years, Microsoft finally gave it up!
50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
月薪没到30K的程序员必须要背的面试八股,我先啃为敬
allegro对走好的线取消走线的方法
ArrayList basic operation and add element source code
Matrix eigenvalue and eigenvector solution - eigenvalue decomposition (EVD)
PWN beginner level0
STM32L4系列单片机ADC通过内部参考电压精确计算输入电压
EMC、EMI、EMS的關系
Relations EMC, EMI, EMS
Talk about SQL optimization
Google Maps API v3~ simply turn off infoindow- Google Map API v3 ~ Simply Close an infowindow?
matlab习题 —— 图像绘制练习
“内窥镜第一股”二闯IPO,去年亏损5个亿,核心产品商业化仍存疑 | IPO速递
Some tests on complementary wasm environment
99乘法表
Application of fsockopen function
信息学奥赛一本通 1361:产生数(Produce)