当前位置:网站首页>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();
}
}
边栏推荐
- Annotations simplify configuration and loading at startup
- Method of intercepting string in shell
- 樹形DP AcWing 285. 沒有上司的舞會
- 求组合数 AcWing 886. 求组合数 II
- 树形DP AcWing 285. 没有上司的舞会
- [rust notes] 11 practical features
- What is an excellent fast development framework like?
- 20220630 learning clock in
- Apache startup failed phpstudy Apache startup failed
- Find the combination number acwing 886 Find the combination number II
猜你喜欢
LeetCode 57. 插入区间
On the setting of global variable position in C language
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Six dimensional space (C language)
LeetCode 1089. 复写零
Slice and index of array with data type
Memory search acwing 901 skiing
LeetCode 715. Range 模块
Gif remove blank frame frame number adjustment
22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
随机推荐
Arbre DP acwing 285. Un bal sans patron.
LeetCode 75. 颜色分类
TP5 order multi condition sort
Deeply understand the underlying data structure of MySQL index
Introduction to the usage of getopts in shell
AcWing 785. 快速排序(模板)
Education informatization has stepped into 2.0. How can jnpf help teachers reduce their burden and improve efficiency?
常见渗透测试靶场
Methods of checking ports according to processes and checking processes according to ports
Noip 2002 popularity group selection number
22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
状态压缩DP AcWing 91. 最短Hamilton路径
Slice and index of array with data type
[rust notes] 05 error handling
记忆化搜索 AcWing 901. 滑雪
[rust notes] 08 enumeration and mode
Format - C language project sub file
Life cycle of Servlet
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework