当前位置:网站首页>AcWing 787. 归并排序(模板)
AcWing 787. 归并排序(模板)
2022-07-03 08:49:00 【Sasakihaise_】
【归并排序】和快排一样也是分治的思想
(1)确定分界点
(2)分界点两侧分别排序
(3)合并两个排好序的数组
(4)复制回原数组中
妥妥的O(nlogn)的时间复杂度,合并需要用O(n)的时间,共logn层
package AcWing.course;
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class _787_归并排序_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();
}
}
边栏推荐
- 树形DP AcWing 285. 没有上司的舞会
- Shell script kills the process according to the port number
- I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
- 【Rust 笔记】11-实用特型
- [concurrent programming] explicit lock and AQS
- Deep parsing JVM memory model
- Drawing maze EasyX library with recursive backtracking method
- [rust notes] 05 error handling
- Mortgage Calculator
- <, < <,>, > > Introduction in shell
猜你喜欢

ES6 promise learning notes

单调栈-84. 柱状图中最大的矩形
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads

Tree DP acwing 285 A dance without a boss

注解简化配置与启动时加载
![[redis] redis persistent RDB vs AOF (source code)](/img/57/b6a86c49cedee31fc00dc5d1372023.jpg)
[redis] redis persistent RDB vs AOF (source code)

Six dimensional space (C language)

Redux - learning notes

Find the combination number acwing 885 Find the combination number I

Binary to decimal, decimal to binary
随机推荐
单调栈-42. 接雨水
Debug debugging - Visual Studio 2022
Log4j2 vulnerability recurrence and analysis
Concurrent programming (VI) ABA problems and solutions under CAS
PHP function date (), y-m-d h:i:s in English case
20220630 learning clock in
Location of package cache downloaded by unity packagemanager
Annotations simplify configuration and loading at startup
[rust notes] 02 ownership
[RPC] RPC remote procedure call
Summary of methods for counting the number of file lines in shell scripts
Unity Editor Extension - drag and drop
Unity editor expansion - draw lines
Deep parsing (picture and text) JVM garbage collector (II)
DOM render mount patch responsive system
状态压缩DP AcWing 91. 最短Hamilton路径
Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
C language student management system based on linked list, super detailed
Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection