当前位置:网站首页>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
边栏推荐
- Machine learning -- model evaluation, selection and verification
- WPF -- implement websocket server
- Theoretical knowledge of digital image (I) (personal analysis)
- Common APIs in string
- Const pointer of C language and parameter passing of main function
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- Basic mathematical knowledge (update)
- Prometheus deployment
- Longest Palindromic Substring
- [C language] Hanoi Tower problem [recursion]
猜你喜欢

Getting started with enterprise distributed crawler framework

In the second half of 2022, the system integration project management engineer certification starts on August 20

Design of air combat game based on qtgui image interface

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

4. Const and difine and the problem of initializing arrays with const and define

数字滤波器设计——Matlab
![[C language] Fibonacci sequence [recursion and iteration]](/img/02/6cff776db583f1b149686e15649d41.png)
[C language] Fibonacci sequence [recursion and iteration]

How many types of rain do you know?

通信网络基础知识01

Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)
随机推荐
plt. What does it mean when linestyle, marker, color equals none in plot()
There is a 'single quotation mark' problem in the string when Oracle inserts data
Multi-Modal Knowledge Graph Construction and Application: A Survey
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
Scene thread allocation in MMO real-time combat games
2022年下半年系统集成项目管理工程师认证8月20日开班
C language array and bubble sort
[C language] function
[C language] header file of complex number four operations and complex number operations
Idea properties file display \u solution of not displaying Chinese
[C language] guessing numbers game [function]
8. Compilation errors of C language and Chinese explanation
Deploy ZABBIX automatically with saltstack
local/chain/run_ tdnn.sh:
[C language] simulation implementation of strlen (recursive and non recursive)
MySQL命令语句(个人总结)
Intermediate soft test (system integration project management engineer) high frequency test site
Implementation of memmove in C language
Implementation of strcat in C language
Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich