当前位置:网站首页>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();
}
}
边栏推荐
- 注解简化配置与启动时加载
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- Vscode connect to remote server
- LeetCode 241. 为运算表达式设计优先级
- AcWing 787. 归并排序(模板)
- [rust notes] 12 closure
- Notes and bugs generated during the use of h:i:s and y-m-d
- XPath实现XML文档的查询
- Try to reprint an article about CSDN reprint
- [rust notes] 02 ownership
猜你喜欢
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads

数字化管理中台+低代码,JNPF开启企业数字化转型的新引擎

LeetCode 871. 最低加油次数

Deep parsing (picture and text) JVM garbage collector (II)

Find the combination number acwing 885 Find the combination number I

LeetCode 715. Range 模块

Binary to decimal, decimal to binary

Binary tree sorting (C language, char type)

Concurrent programming (VI) ABA problems and solutions under CAS

教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
随机推荐
LeetCode 324. 摆动排序 II
20220630 learning clock in
分配异常的servlet
<, < <,>, > > Introduction in shell
Parameters of convolutional neural network
Servlet的生命周期
数位统计DP AcWing 338. 计数问题
Solution of 300ms delay of mobile phone
LeetCode 30. 串联所有单词的子串
精彩回顾|I/O Extended 2022 活动干货分享
教育信息化步入2.0,看看JNPF如何帮助教师减负,提高效率?
高斯消元 AcWing 883. 高斯消元解线性方程组
Markdown learning
[rust notes] 09- special types and generics
LeetCode 241. 为运算表达式设计优先级
Slice and index of array with data type
Notes and bugs generated during the use of h:i:s and y-m-d
Deep parsing JVM memory model
The method of replacing the newline character '\n' of a file with a space in the shell
LeetCode 508. 出现次数最多的子树元素和