当前位置:网站首页>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();
}
}
边栏推荐
- Method of intercepting string in shell
- 22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
- Allocation exception Servlet
- TP5 multi condition sorting
- JS ternary operator - learning notes (with cases)
- C language file reading and writing
- [concurrent programming] atomic operation CAS
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- LeetCode 75. 颜色分类
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
猜你喜欢

状态压缩DP AcWing 291. 蒙德里安的梦想

Parameters of convolutional neural network

Find the combination number acwing 886 Find the combination number II

LeetCode 241. 为运算表达式设计优先级

Redux - learning notes
![[rust notes] 02 ownership](/img/f7/74f8ea3bd697957f9ebfa3e1513fda.png)
[rust notes] 02 ownership

低代码起势,这款信息管理系统开发神器,你值得拥有!

DOM render mount patch responsive system

ES6 promise learning notes

剑指 Offer II 091. 粉刷房子
随机推荐
22-05-26 Xi'an interview question (01) preparation
[concurrent programming] concurrent security
Monotonic stack -84 The largest rectangle in the histogram
JS ternary operator - learning notes (with cases)
我们有个共同的名字,XX工
What is the difference between sudo apt install and sudo apt -get install?
Wonderful review | i/o extended 2022 activity dry goods sharing
Low code momentum, this information management system development artifact, you deserve it!
Use of sort command in shell
[concurrent programming] atomic operation CAS
樹形DP AcWing 285. 沒有上司的舞會
数位统计DP AcWing 338. 计数问题
Deep parsing JVM memory model
Try to reprint an article about CSDN reprint
C language file reading and writing
Notes and bugs generated during the use of h:i:s and y-m-d
LeetCode 535. TinyURL 的加密与解密
Parameters of convolutional neural network
Analysis of Alibaba canal principle
Servlet的生命周期