当前位置:网站首页>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();
}
}
边栏推荐
- 22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- Monotonic stack -84 The largest rectangle in the histogram
- Concurrent programming (V) detailed explanation of atomic and unsafe magic classes
- 树形DP AcWing 285. 没有上司的舞会
- String splicing method in shell
- Get the link behind? Parameter value after question mark
- C language file reading and writing
- [concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
- [rust notes] 12 closure
猜你喜欢

I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made

请求参数的发送和接收

20220630 learning clock in

Concurrent programming (III) detailed explanation of synchronized keyword

Monotonic stack -84 The largest rectangle in the histogram
![[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle](/img/b7/7bf2a4a9ab51364352aa5e0a196b6d.jpg)
[MySQL] MySQL Performance Optimization Practice: introduction of database lock and index search principle

Binary to decimal, decimal to binary
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads

Unity editor expansion - draw lines

Gaussian elimination acwing 883 Gauss elimination for solving linear equations
随机推荐
LeetCode 324. 摆动排序 II
The method for win10 system to enter the control panel is as follows:
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
[rust notes] 05 error handling
求组合数 AcWing 885. 求组合数 I
createjs easeljs
[rust notes] 07 structure
Eating fruit
Dom4j遍历和更新XML
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
producer consumer problem
The difference between if -n and -z in shell
Convert video to GIF
【Rust 笔记】10-操作符重载
Collection interface
Gif remove blank frame frame number adjustment
Solution of 300ms delay of mobile phone
Message queue for interprocess communication
Sending and receiving of request parameters
请求参数的发送和接收