当前位置:网站首页>Merge sort
Merge sort
2022-06-29 02:48:00 【Madness makes freedom】
2—— Path merging and sorting , Sort the sequence in pairs , share n/2 Group ; Then merge and sort these groups in pairs , obtain n/4 Group , And so on , Until there is only one group left , At this point, the group is orderly .
In pairs , You can use the idea of dichotomy to group .
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);
}
}
Merge sort
void my_merge(int *a,int l1,int r1,int l2,int r2)// Two ordered sequences , Merge into an ordered sequence ,tow point : Combine two ordered sequences into one ,O(n)
void my_merge(int *a,int l1,int r1,int l2,int r2)// Two ordered sequences , Merge into an ordered sequence
{
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];
}
/**
Merge sort
tow point : Combine two ordered sequences into one ,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)// Two ordered sequences , Merge into an ordered sequence
{
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;
}边栏推荐
- 99 multiplication table
- LinkedList学习
- Overview of PMP project management
- 使用gdb添加断点的几种方式
- Informatics Olympiad all in one 1361: production | Luogu P1037 [noip2002 popularization group] production
- LinkedList learning
- SQL training 01
- [Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre
- 对补wasm环境的一些测试
- 【无标题】
猜你喜欢

Introduction to openresty

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

Pvcreate ASM disk causes abnormal recovery of ASM disk group - sparing separation

Use photoshop2022 to create a wonderful gradient effect for pictures

EMC、EMI、EMS的关系

Ctfhub web password weak password
![[linear algebra] 1.1 second and third order determinants](/img/ea/70b59c64d3287a887e371a9181fe45.png)
[linear algebra] 1.1 second and third order determinants

双击事件与单击事件的那些事

LabVIEW jump to web page

高并发的理解与设计方案
随机推荐
Eight difficulties of embedded C language
【无标题】
Oracle recovery tools actual batch bad block repair
Bluetooth solution | Lenz technology Amazon direct connected magic lantern solution
STM32L4系列单片机ADC通过内部参考电压精确计算输入电压
PMP商业分析概述
On the fact that lambda expressions cannot handle recursion
Trigonometric function calculation
50 lectures on practical application of R language (34) - practical application cases of curve separation (with R language code)
PHP system function
[Algèbre linéaire] 1.1 déterminant du deuxième et du troisième ordre
Install mysql5.7 and change the password
sql连续登录问题
After today, I look forward to the new year's eve of the year of the rabbit
Apache does not parse PHP files, but directly displays the source code
Tortoise 没有显示绿色图标
MySQL queries the data of today, yesterday, this week, last week, this month, last month, this quarter, last quarter, this year, last year
Summary of several days
使用gdb添加断点的几种方式
Handling method of occasional error reporting on overseas equipment