当前位置:网站首页>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();
}
}
边栏推荐
- 教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
- How to delete CSDN after sending a wrong blog? How to operate quickly
- AcWing 785. 快速排序(模板)
- [rust notes] 11 practical features
- 分配异常的servlet
- Common DOS commands
- LeetCode 508. 出现次数最多的子树元素和
- TP5 order multi condition sort
- Solution of 300ms delay of mobile phone
- Life cycle of Servlet
猜你喜欢
随机推荐
Mortgage Calculator
Sending and receiving of request parameters
Alibaba canal actual combat
Convert video to GIF
Servlet的生命周期
一个优秀速开发框架是什么样的?
Vscode connect to remote server
First Servlet
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
剑指 Offer II 091. 粉刷房子
file_ put_ contents
Deeply understand the underlying data structure of MySQL index
20220630 learning clock in
LeetCode 871. 最低加油次数
Character pyramid
LeetCode 30. 串联所有单词的子串
Life cycle of Servlet
Apache startup failed phpstudy Apache startup failed
树形DP AcWing 285. 没有上司的舞会
On a un nom en commun, maître XX.








