当前位置:网站首页>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
边栏推荐
- 9. Pointer of C language (4) pointer and one-dimensional array, pointer operation
- C+ + core programming
- Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
- Edge detection and connection of image segmentation realized by MATLAB
- English Translation Spanish - batch English Translation Spanish tools free of charge
- 9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?
- Labelme (I)
- 6. Functions of C language, why functions are needed, how functions are defined, and the classification of functions
- 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
- JS preventdefault() keyboard input limit onmousewheel stoppropagation stop event propagation
猜你喜欢

中国能否在元宇宙的未来发展中取得突破,占领高地?

为什么客户支持对SaaS公司很重要?

利用STM32的HAL库驱动1.54寸 TFT屏(240*240 ST7789V)
![[C language] function](/img/81/c185e2bb5eccc13433483f9558f52a.png)
[C language] function

XOR operation and its usage

What is the process of swing event processing?

9. Pointer of C language (3) classic program, exchange the value of two numbers for deep analysis, (easy to understand), are formal parameters and arguments a variable?

9. Pointer of C language (4) pointer and one-dimensional array, pointer operation

熊市下PLATO如何通过Elephant Swap,获得溢价收益?

Cdga | how can the industrial Internet industry do a good job in data governance?
随机推荐
Basic usage of docker
熊市下PLATO如何通过Elephant Swap,获得溢价收益?
3、 Are formal and actual parameters in a programming language variables?
C language function
Handan, Hebei: expand grassroots employment space and help college graduates obtain employment
Source code analysis of scripy spider
通配符 SSL/TLS 证书
What is the process of swing event processing?
Advanced notes (Part 2)
The cloud native programming challenge is hot, with 510000 bonus waiting for you to challenge!
“中国网事·感动2022”二季度网络感动人物评选结果揭晓
English translation Italian - batch English translation Italian tools free of charge
Multi-Modal Knowledge Graph Construction and Application: A Survey
Power Bi 2021 calendar DAX code
数字图像理论知识(一)(个人浅析)
English translation Arabic - batch English translation Arabic tools free of charge
[C language] initial C language reflection and summary
云原生编程挑战赛火热开赛,51 万奖金等你来挑战!
English translation Portuguese - batch English conversion Portuguese - free translation and conversion of various languages
Design of air combat game based on qtgui image interface