当前位置:网站首页>AcWing 785. Quick sort (template)
AcWing 785. Quick sort (template)
2022-07-03 09:01:00 【Sasakihaise_】
【 Quick line up 】 Divide and conquer thoughts
(1) Define the boundary x:q[l], q[(l + r) / 2], Random ( Better choose the middle )
(2) Adjustment interval : Less than or equal to x,x, Greater than x
(3) Recursively handle the left and right ends :
Average complexity O(nlogn), The worst O(n^2), Unstable sequencing
import java.io.StreamTokenizer;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class Main {
static int n;
static int[] arr;
static StreamTokenizer st;
static int nextInt() throws Exception{
st.nextToken();
return (int)st.nval;
}
static void quickSort(int l, int r){
if(l >= r) return;
int i = l, j = r, pivot = arr[(l + r) >>> 1];
while(i <= j){
while(arr[j] > pivot) j--;
while(arr[i] < pivot) i++;
if(i <= j){
int t = arr[i];
arr[i] = arr[j];
arr[j] = t;
i++; j--;
}
}
quickSort(l, j);
quickSort(i, r);
}
public static void main(String[] args) throws Exception{
PrintWriter pw = new PrintWriter(System.out);
st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
n = nextInt();
arr = new int[n];
for(var i = 0; i < n; i++) arr[i] = nextInt();
quickSort(0, n - 1);
for(var x: arr){
pw.print(x);
pw.print(" ");
}
pw.flush();
}
}
边栏推荐
- 我们有个共同的名字,XX工
- AcWing 785. 快速排序(模板)
- Facial expression recognition based on pytorch convolution -- graduation project
- Dom4j traverses and updates XML
- [rust notes] 07 structure
- LeetCode 75. 颜色分类
- 干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
- too many open files解决方案
- Deep parsing (picture and text) JVM garbage collector (II)
- [rust notes] 12 closure
猜你喜欢
Parameters of convolutional neural network
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
TP5 multi condition sorting
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
too many open files解决方案
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
On a un nom en commun, maître XX.
Facial expression recognition based on pytorch convolution -- graduation project
求组合数 AcWing 886. 求组合数 II
Monotonic stack -84 The largest rectangle in the histogram
随机推荐
低代码起势,这款信息管理系统开发神器,你值得拥有!
Binary tree sorting (C language, char type)
How to use Jupiter notebook
Method of intercepting string in shell
Methods of checking ports according to processes and checking processes according to ports
记忆化搜索 AcWing 901. 滑雪
Memory search acwing 901 skiing
LeetCode 515. 在每个树行中找最大值
AcWing 787. 归并排序(模板)
AcWing 786. 第k个数
樹形DP AcWing 285. 沒有上司的舞會
Divide candy (circular queue)
ES6 promise learning notes
First Servlet
Es8 async and await learning notes
Use of sort command in shell
Common DOS commands
DOM render mount patch responsive system
[rust notes] 07 structure
Redux - learning notes