当前位置:网站首页>排序之归并排序
排序之归并排序
2022-06-22 14:26:00 【Stephen_Curry___】
归并排序
归并排序的主要算法思想也是分治思想,实现归并排序可以分为三部分:
1.确定分界点mid=(l+r)/2;
2.递归排序;
3.归并左右两个区间(核心步骤);
代码板子如下:
#include<iostream>
using namespace std;
const int N = 1e6+10;
int n;
int q[N],tmp[N];
void merge_sprt(int q[N],int l,int r)
{
if(l>=r)return;
int mid=(l+r)/2;
merge_sort(q,l,mid),merge_sort(q,mid+1,r);
//对整个数组进行区间划分,每次都是区间长度/2
//所以要划分logn次,区间长度为n,所以时间复杂度为(nlogn)
int k=0,i=l,j=mid+1;
while(i<=mid&&j<=r)
{
if(q[i]<=q[j])tmp[k++]=q[i++];
else tmp[k++]=q[j++];
}
//当一个数组的指针扫完该数组之后处理另一个数组剩余的元素
while(i<=mid)tmp[k++]=q[i++];
while(j<=r)tmp[k++]=q[j++];
//最后将标记数组更新到答案数组内
for(int i=l,j=0;i<=r;i++,j++)
q[i]=tmp[j];
}
int main()
{
scanf("%d",&n);
for(int i=0;i<n;i++)scanf("%d",&q[i]);
merge_sort(q,0,n-1);
for(int i=0;i<n;i++)printf("%d ",q[i]);
}
后续会再补上图帮助理解哒!!!
边栏推荐
- Tdengine connector goes online Google Data Studio store
- ROS2前置基础教程 | 使用CMakeLists.txt编译ROS2节点
- "Forget to learn again" shell process control - 38. Introduction to while loop and until loop
- 得物技术复杂 C 端项目的重构实践
- 模板特例化 template<>
- I rely on the sideline to buy a house in full one year: the industry you despise will make a lot of money in the next ten years!
- Flutter video Le lecteur écoute et joue automatiquement la prochaine chanson
- 宏源期货开户安全么?宏源期货公司可以降低手续费?
- flutter video_player實現監聽和自動播放下一首歌曲
- 加密市场进入寒冬,是“天灾”还是“人祸”?
猜你喜欢

Simulation Keil et vspd

The summary of high concurrency experience under the billion level traffic for many years is written in this book without reservation

得物技术复杂 C 端项目的重构实践

专业“搬砖”老司机总结的 12 条 SQL 优化方案,非常实用!

How MySQL modifies the storage engine to InnoDB

mysql如何将字段修改为not null

本周四晚19:00战码先锋第7期直播丨三方应用开发者如何为开源做贡献

2022年失业的人多吗?今年是不是特别难找工作?

NF-ResNet:去掉BN归一化,值得细读的网络信号分析 | ICLR 2021

模板特例化 template<>
随机推荐
KEIL仿真和vspd
加密市场进入寒冬,是“天灾”还是“人祸”?
得物App数据模拟平台的探索和实践
Runmaide medical passed the hearing: Ping An capital was a shareholder with a loss of 630million during the year
那些没考上大学的人,后来过的怎样
Charles 乱码问题解决
Token processing during API encapsulation
多年亿级流量下的高并发经验总结,都毫无保留地写在了这本书中
Ml notes matrix fundamental, gradient descent
润迈德医疗通过聆讯:年内亏损6.3亿 平安资本是股东
Flutter video Le lecteur écoute et joue automatiquement la prochaine chanson
visual studio开发过程中常见操作
All famous network platforms in the world
Self inspection is recommended! The transaction caused by MySQL driver bug is not rolled back. Maybe you are facing this risk!
Those confusing user state & kernel state
[graduation project] Research on emotion analysis based on semi supervised learning and integrated learning
架构师之路,从「存储选型」起步
keil MDK 中使用虚拟串口调试串口
网站存在的价值是什么?为什么要搭建独立站
数据库连接池:压力测试