当前位置:网站首页>Merge sort template
Merge sort template
2022-07-28 20:14:00 【Tiredd】
Title Description :

The code is as follows :
#include <iostream>
using namespace std;
const int N = 100010;
int n, a[N], q[N];
void merge_sort(int l, int r)
{
if(l >= r) return ;
int mid = l + r >> 1;
merge_sort(l, mid), merge_sort(mid + 1, r);
int i = l, j = mid + 1, t = 0;
while(i <= mid && j <= r)
{
if(a[i] >= a[j]) q[t ++ ] = a[j ++ ];
else q[t ++ ] = a[i ++ ];
}
while(i <= mid) q[t ++ ] = a[i ++ ];
while(j <= r) q[t ++ ] = a[j ++ ];
for(i = l, j = 0; i <= r; i ++, j ++ )
a[i] = q[j];
}
int main()
{
scanf("%d", &n);
for(int i = 0; i < n; i ++ ) scanf("%d", &a[i]);
merge_sort(0, n - 1);
for(int i = 0; i < n; i ++ ) printf("%d ", a[i]);
}Extended topics : Reverse order to quantity
subject :

Ideas : When the two ways merge, the two sequences will be orderly , At this point, it can be in the process of merging , Carry out statistical reverse order pair , Then the complexity will become nlog(n), See the code for details
// topic 4
#include <iostream>
using namespace std;
const int N = 1e6;
int n, a[N], q[N];
long long res = 0;
void merge_sort(int l, int r)
{
if(l >= r) return ;
int mid = l + r >> 1;
merge_sort(l, mid), merge_sort(mid + 1, r);
int i = l, j = mid + 1, t = 0;
while(i <= mid && j <= r)
{
if(a[i] <= a[j])
q[t ++ ] = a[i ++ ];
else
{
res += mid - i + 1; // The core
q[t ++ ] = a[j ++ ];
}
}
while(i <= mid) q[t ++ ] = a[i ++ ];
while(j <= r) q[t ++ ] = a[j ++ ];
for(int i = l, j = 0; i <= r; i ++, j ++ )
a[i] = q[j];
}
int main()
{
cin >> n;
for(int i = 0; i < n; i ++ ) cin >> a[i];
merge_sort(0, n - 1);
cout << res << endl;
return 0;
}over
边栏推荐
- 1、 Relationship among CPU, memory and hard disk
- C language pointer and two-dimensional array
- 党员故事|李青艾用漫画带动农民增收致富
- Source insight project import and use tutorial
- flask_ Mail source code error
- Zfoo adds routes similar to mydog
- Implementation of strstr in C language
- Function fitting based on MATLAB
- There is a 'single quotation mark' problem in the string when Oracle inserts data
- English translation Italian - batch English translation Italian tools free of charge
猜你喜欢
![[C language] summary of methods for solving the greatest common divisor](/img/38/3a099948ebf51fd0da3076f71f9dad.png)
[C language] summary of methods for solving the greatest common divisor

Longest Palindromic Substring

83.(cesium之家)cesium示例如何运行

Sprint for golden nine and silver ten, stay up at night for half a month, collect 1600 real interview questions from Android post of major manufacturers

Communication learning static routing across regional networks

How to use pycharm to quickly create a flask project

Source insight project import and use tutorial

Prometheus deployment

Overcome the "fear of looking at teeth", and we use technology to change the industry
![[C language] step jumping problem [recursion]](/img/0c/32870484e89b494e41068f7c38b08e.png)
[C language] step jumping problem [recursion]
随机推荐
Item exception handling in SSM
There is a 'single quotation mark' problem in the string when Oracle inserts data
[C language] Fibonacci sequence [recursion and iteration]
Return and job management of saltstack
Implementation of memcpy in C language
Getting started with enterprise distributed crawler framework
The privatized instant messaging platform protects the security of enterprise mobile business
[in depth study of 4g/5g/6g topics -44]: urllc-15 - in depth interpretation of 3GPP urllc related protocols, specifications and technical principles -9-low delay technology -3-non slot scheduling mini
【实验分享】CCIE—BGP反射器实验
English Translation Spanish - batch English Translation Spanish tools free of charge
软考中级(系统集成项目管理工程师)高频考点
mmo及时战斗游戏中的场景线程分配
Know small and medium LAN WLAN
flask_ Mail source code error
通配符 SSL/TLS 证书
XOR operation and its usage
Common modules of saltstack
Implementation of memmove in C language
C language operators and input and output
河北邯郸:拓展基层就业空间 助力高校毕业生就业