当前位置:网站首页>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();
}
}
边栏推荐
- PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
- 樹形DP AcWing 285. 沒有上司的舞會
- Summary of methods for counting the number of file lines in shell scripts
- Divide candy (circular queue)
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- How to deal with the core task delay caused by insufficient data warehouse resources
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- TP5 multi condition sorting
- [concurrent programming] concurrent security
- 推荐一个 yyds 的低代码开源项目
猜你喜欢

Concurrent programming (VI) ABA problems and solutions under CAS

Find the combination number acwing 886 Find the combination number II

LeetCode 438. 找到字符串中所有字母异位词

Sending and receiving of request parameters

Concurrent programming (III) detailed explanation of synchronized keyword

Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)

Arbre DP acwing 285. Un bal sans patron.

AcWing 788. 逆序对的数量

DOM 渲染系统(render mount patch)响应式系统

剑指 Offer II 029. 排序的循环链表
随机推荐
PHP uses foreach to get a value in a two-dimensional associative array (with instances)
LeetCode 715. Range 模块
createjs easeljs
LeetCode 57. 插入区间
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
The method of replacing the newline character '\n' of a file with a space in the shell
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
ES6 promise learning notes
Notes and bugs generated during the use of h:i:s and y-m-d
[rust notes] 06 package and module
注解简化配置与启动时加载
[rust notes] 08 enumeration and mode
AcWing 786. 第k个数
<, < <,>, > > Introduction in shell
Dom4j traverses and updates XML
How to delete CSDN after sending a wrong blog? How to operate quickly
On the difference and connection between find and select in TP5 framework
[rust note] 10 operator overloading
LeetCode 513. 找树左下角的值
20220630学习打卡