当前位置:网站首页>[2. merge sort]
[2. merge sort]
2022-06-22 02:41:00 【Little silly bird_ coding】
Merging is different from fast sorting :
Quick sort :
- The dividing point is one of the random arrays
CountCome to share , Make the left side all <= The number of , On the right >= The number of (The number)- Finish first , On the two sides of recursion respectively
Merge sort :
- First recurse both sides , In the process of merging
- The dividing point is
The middle of the whole array(Subscript value)
Ideas :( Double pointer algorithm )
- Determine the cut-off point mid = (l + r) / 2
- Sort recursively, respectively , Left and right
- At this point, after the recursion is completed , Both sides are ordered arrays , At this point, two arrays
Become(above all > Of)
Schematic diagram
(1) recursive ( Recursion from top to bottom )
(2) Become ( Recursion is recursion from top to bottom , A merge is a bottom-up merge )
subject
Give you a length of n The whole number sequence of .
Please use merge sort to sort this sequence from small to large .
And output the ordered sequence in order .
Input format
There are two lines of input , The first line contains integers n.
The second line contains n It's an integer ( All integers are in 1∼109 Within the scope of ), Represents the whole sequence of numbers .
Output format
The output is one line , contain n It's an integer , It's an ordered sequence .
Data range
1 ≤ n ≤ 100000
sample input :5 3 1 2 4 5sample output :
1 2 3 4 5
Code
#include<iostream> using namespace std; const int N = 1000010; int q[N]; int temp[N]; // Defines another temporary array , So the space complexity is zero O(1); int n; void merage_sort(int q[],int l,int r) { if(l>=r)return; int mid = l+r>>1; merage_sort(q, l, mid); // recursive merage_sort(q,mid+1, r); // After recursion , At this point, it becomes two ordered sequences , Namely [l, >mid] and [mid+1, r]. int k = 0; int i = l,j=mid+1; // The starting elements of these two arrays are l and mid+1 while(i<=mid && j<=r) // When any one of the arrays is traversed , Stop program { if(q[i]<=q[j]) temp[k ++] = q[i ++]; else temp[k ++] = q[j ++]; } while(i<=mid) temp[k ++] = q[i ++]; // At this point, an array has been traversed , And the other array has > The remaining , The rest are arranged in order , So just connect the following paragraph to temp On while(j<=r) temp[k ++] = q[j ++]; for(int i =l ,j = 0;i<=r;i++,j++)q[i] = temp[j]; // This is a letter l, Not numbers 1, Set the value of the temporary array ,> After copying to the original array q in } int main() { scanf("%d",&n); for(int i = 0;i < n;i++) scanf("%d",&q[i]); merage_sort(q,0,n-1); for(int i = 0;i<n;i++) printf(" %d",q[i]); return 0; }
边栏推荐
- Dernière publication: neo4j Graph Data Science GDS 2.0 et aurads ga
- Paper notes: multi label learning ackel
- 【6. 高精度乘法】
- PMP pre exam guide on June 25, you need to do these well
- 【3.整数与浮点数二分】
- Automated tools - monitoring file changes
- EMC rectification tips
- The brand, products and services are working together. What will Dongfeng Nissan do next?
- 【9. 子矩阵和】
- discuz! Bug in the VIP plug-in of the forum repair station help network: when the VIP member expires and the permanent member is re opened, the user group does not switch to the permanent member group
猜你喜欢

What is a neural network

EMC rectification tips

Asemi fast recovery diode FR107 parameter, FR107 real object, FR107 application

Game jam development cycle

Development of power plant compliance test system with LabVIEW

【7. 高精度除法】

LabVIEW开发发电厂合规性测试系统

On Monday, I asked the meaning of the | -leaf attribute?

DAST black box vulnerability scanner part 4: scanning performance
![[proteus simulation] INT0 and INT1 interrupt count](/img/0b/b3f5adb97046d927e501ea34deb3d9.png)
[proteus simulation] INT0 and INT1 interrupt count
随机推荐
fatal error: png++/png. Hpp: no that file or directory
Creating and extending XFS file system based on LVM
ModuleNotFoundError: No module named ‘torchvision. datasets‘; ‘ torchvision‘ is not a package
Graphconnect 2022 at a glance
Asemi fast recovery diode FR107 parameter, FR107 real object, FR107 application
rt_ Message queue of thread
Wechat applet film and television comment exchange platform system graduation design (2) applet function
Get to know unity3d (project structure, third-party plug-in of probuilder)
国产品牌OPPO官方最新出品!这份PPT报告!真刷新我对它认知了
How to select the appropriate version of neo4j (version 2022)
idea----bookmark
理财产品赎回确认日是什么意思?
【一起上水硕系列】Day Two
What is a neural network
Chapter 24 image and video processing based on Simulink -- matlab in-depth learning and practical collation
MATLAB 学习笔记(5)MATLAB 数据的导入和导出
Relative references must start with either “/“, “./“, or “../“.
MySQL recursively finds the tree structure. This method is very practical!
Implementation differences between import and require in browser and node environments
How to use tensorboard add_ histogram

