当前位置:网站首页>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();
}
}
边栏推荐
- Really explain the five data structures of redis
- Arbre DP acwing 285. Un bal sans patron.
- [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
- JS non Boolean operation - learning notes
- Deep parsing (picture and text) JVM garbage collector (II)
- Memory search acwing 901 skiing
- Find the combination number acwing 885 Find the combination number I
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- Sending and receiving of request parameters
- URL backup 1
猜你喜欢

Mortgage Calculator

Monotonic stack -503 Next bigger Element II

拯救剧荒,程序员最爱看的高分美剧TOP10

MySQL three logs

Markdown learning

Try to reprint an article about CSDN reprint

On a un nom en commun, maître XX.

How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework

Query XML documents with XPath

状态压缩DP AcWing 91. 最短Hamilton路径
随机推荐
[concurrent programming] collaboration between threads
浅谈企业信息化建设
AcWing 787. 归并排序(模板)
too many open files解决方案
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
常见渗透测试靶场
Using DLV to analyze the high CPU consumption of golang process
PHP function date (), y-m-d h:i:s in English case
How to deal with the core task delay caused by insufficient data warehouse resources
MySQL index types B-tree and hash
[concurrent programming] atomic operation CAS
低代码起势,这款信息管理系统开发神器,你值得拥有!
状态压缩DP AcWing 91. 最短Hamilton路径
DOM 渲染系统(render mount patch)响应式系统
状态压缩DP AcWing 291. 蒙德里安的梦想
Find the combination number acwing 885 Find the combination number I
JS ternary operator - learning notes (with cases)
[rust notes] 07 structure
传统办公模式的“助推器”,搭建OA办公系统,原来就这么简单!