当前位置:网站首页>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
边栏推荐
- Return and job management of saltstack
- Special draft of Mir | common sense knowledge and reasoning: representation, acquisition and application (deadline on October 31)
- Store and guarantee rancher data based on Minio objects
- MySQL命令语句(个人总结)
- Read how to deploy highly available k3s with external database
- KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书
- Richpedia: A Large-Scale, Comprehensive Multi-Modal Knowledge Graph
- [C language] Gobang game [array and function]
- Tencent cloud deployment lamp_ Experience of building a station
- How many types of rain do you know?
猜你喜欢

JS batch add event listening onclick this event delegate target currenttarget onmouseenter OnMouseOver

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

Information management system and games based on C language

Hebei: stabilizing grain and expanding beans to help grain and oil production improve quality and efficiency
![[C language] function](/img/81/c185e2bb5eccc13433483f9558f52a.png)
[C language] function

数字图像理论知识(一)(个人浅析)

KubeEdge发布云原生边缘计算威胁模型及安全防护技术白皮书

The results of the second quarter online moving people selection of "China Internet · moving 2022" were announced

Use Hal Library of STM32 to drive 1.54 inch TFT screen (240*240 st7789v)

Stories of Party members | Li qingai uses cartoons to drive farmers to increase income and become rich
随机推荐
ssm中项目异常处理
Why is there no log output in the telnet login interface?
WFST decoding process
Sequential linear table - practice in class
flask_ Mail source code error
Failed to install app-debug. apk: Failure [INSTALL_FAILED_TEST_ONLY: installPackageLI]
Solve flask integration_ Error reporting in restplus
C+ + core programming
Store and guarantee rancher data based on Minio objects
为什么客户支持对SaaS公司很重要?
Use of strtok and strError
Common modules of saltstack
6. Functions of C language, why functions are needed, how functions are defined, and the classification of functions
Application skills of programming rising and falling edge instructions of botu 1200/1500plc (bool array)
WPF--实现WebSocket服务端
9. Pointer of C language (4) pointer and one-dimensional array, pointer operation
Theoretical knowledge of digital image (I) (personal analysis)
2. Floating point number, the difference between float and double in C language and how to choose them
There is a 'single quotation mark' problem in the string when Oracle inserts data
9. Pointer of C language (2) wild pointer, what is wild pointer, and the disadvantages of wild pointer