当前位置:网站首页>AcWing 787. Merge sort (template)
AcWing 787. Merge sort (template)
2022-07-03 09:01:00 【Sasakihaise_】
【 Merge sort 】 Like fast platoon, it is also the idea of partition
(1) Determine the cut-off point
(2) The two sides of the dividing point are sorted respectively
(3) Merge two ordered arrays
(4) Copy back to the original array
Proper O(nlogn) Time complexity of , Required for consolidation O(n) Time for , common logn layer
package AcWing.course;
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class _787_ Merge sort _1 {
// 8:38 8:50
static int n;
static int[] arr, tmp;
static StreamTokenizer st;
static int nextInt() throws Exception {
st.nextToken();
return (int)st.nval;
}
static void mergeSort(int l, int r){
if(l >= r) return;
int mid = (l + r) >>> 1, i = l, j = mid + 1, k = 0;
mergeSort(l, mid); mergeSort(mid + 1, r);
while(i <= mid && j <= r){
if(arr[i] <= arr[j]) tmp[k++] = arr[i++];
else tmp[k++] = arr[j++];
}
while(i <= mid) tmp[k++] = arr[i++];
while(j <= r) tmp[k++] = arr[j++];
for(i = l, j = 0; j < k; j++, i++) arr[i] = tmp[j];
}
public static void main(String[] args) throws Exception {
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
PrintWriter pw = new PrintWriter(System.out);
n = nextInt();
arr = new int[n];
tmp = new int[n];
for(var i = 0; i < n; i++) arr[i] = nextInt();
mergeSort(0, n - 1);
for(var x: arr){
pw.print(x);
pw.print(" ");
}
pw.flush();
}
}
边栏推荐
- The method for win10 system to enter the control panel is as follows:
- Deeply understand the underlying data structure of MySQL index
- LeetCode 324. 摆动排序 II
- 常见渗透测试靶场
- 教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
- Using DLV to analyze the high CPU consumption of golang process
- too many open files解决方案
- Sending and receiving of request parameters
- [rust notes] 09- special types and generics
- 请求参数的发送和接收
猜你喜欢

PHP uses foreach to get a value in a two-dimensional associative array (with instances)

干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了

Complex character + number pyramid

excel一小时不如JNPF表单3分钟,这样做报表,领导都得点赞!

树形DP AcWing 285. 没有上司的舞会

Concurrent programming (V) detailed explanation of atomic and unsafe magic classes

状态压缩DP AcWing 291. 蒙德里安的梦想

网络安全必会的基础知识

浅谈企业信息化建设

Find the combination number acwing 885 Find the combination number I
随机推荐
一个优秀速开发框架是什么样的?
我们有个共同的名字,XX工
Life cycle of Servlet
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
The method of replacing the newline character '\n' of a file with a space in the shell
Vscode connect to remote server
[concurrent programming] collaboration between threads
The method for win10 system to enter the control panel is as follows:
Concurrent programming (VI) ABA problems and solutions under CAS
MySQL three logs
Facial expression recognition based on pytorch convolution -- graduation project
too many open files解决方案
String splicing method in shell
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
LeetCode 513. 找树左下角的值
Allocation exception Servlet
高斯消元 AcWing 883. 高斯消元解线性方程组
Analysis of Alibaba canal principle
Complex character + number pyramid
求组合数 AcWing 885. 求组合数 I