当前位置:网站首页>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();
}
}
边栏推荐
- 22-05-26 Xi'an interview question (01) preparation
- [concurrent programming] concurrent tool class of thread
- Find the combination number acwing 886 Find the combination number II
- [rust notes] 08 enumeration and mode
- Wonderful review | i/o extended 2022 activity dry goods sharing
- Binary to decimal, decimal to binary
- Phpstudy 80 port occupied W10 system
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- Method of intercepting string in shell
- PHP mnemonic code full text 400 words to extract the first letter of each Chinese character
猜你喜欢
22-06-28 西安 redis(02) 持久化机制、入门使用、事务控制、主从复制机制
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
Character pyramid
剑指 Offer II 091. 粉刷房子
20220630学习打卡
LeetCode 57. 插入区间
Deep parsing (picture and text) JVM garbage collector (II)
Redux - learning notes
数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎
Concurrent programming (III) detailed explanation of synchronized keyword
随机推荐
JS non Boolean operation - learning notes
Redux - learning notes
[set theory] order relation (total order relation | total order set | total order relation example | quasi order relation | quasi order relation theorem | bifurcation | quasi linear order relation | q
AcWing 787. 归并排序(模板)
PHP function date (), y-m-d h:i:s in English case
Notes and bugs generated during the use of h:i:s and y-m-d
[rust notes] 05 error handling
TP5 multi condition sorting
[concurrent programming] working mechanism and type of thread pool
Parameters of convolutional neural network
即时通讯IM,是时代进步的逆流?看看JNPF怎么说
状态压缩DP AcWing 91. 最短Hamilton路径
URL backup 1
[rust notes] 08 enumeration and mode
On the setting of global variable position in C language
Debug debugging - Visual Studio 2022
Deep parsing (picture and text) JVM garbage collector (II)
干货!零售业智能化管理会遇到哪些问题?看懂这篇文章就够了
DOM 渲染系统(render mount patch)响应式系统
请求参数的发送和接收