当前位置:网站首页>AcWing 785. 快速排序(模板)
AcWing 785. 快速排序(模板)
2022-07-03 08:49:00 【Sasakihaise_】
【快排】分治思想
(1)确定边界x:q[l], q[(l + r) / 2], 随机(最好选中间)
(2)调整区间:小于等于x,x,大于x
(3)递归处理左右两端:
平均复杂度O(nlogn),最坏O(n^2),不稳定的排序
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();
}
}
边栏推荐
- Facial expression recognition based on pytorch convolution -- graduation project
- [rust notes] 12 closure
- Redux - learning notes
- 状态压缩DP AcWing 91. 最短Hamilton路径
- MySQL three logs
- Escape from heaven and forget what he suffered. In ancient times, it was called the punishment of escape from heaven. Article collection
- How to deal with the core task delay caused by insufficient data warehouse resources
- Methods of using arrays as function parameters in shell
- [concurrent programming] atomic operation CAS
- [rust notes] 13 iterator (Part 1)
猜你喜欢
单调栈-503. 下一个更大元素 II
Phpstudy 80 port occupied W10 system
MySQL three logs
求组合数 AcWing 886. 求组合数 II
Binary to decimal, decimal to binary
Analysis of Alibaba canal principle
TP5 multi condition sorting
I made mistakes that junior programmers all over the world would make, and I also made mistakes that I shouldn't have made
单调栈-42. 接雨水
Markdown learning
随机推荐
Slice and index of array with data type
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
单调栈-503. 下一个更大元素 II
Character pyramid
On the difference and connection between find and select in TP5 framework
Tree DP acwing 285 A dance without a boss
[concurrent programming] explicit lock and AQS
Binary tree traversal (first order traversal. Output results according to first order, middle order, and last order)
剑指 Offer II 091. 粉刷房子
状态压缩DP AcWing 291. 蒙德里安的梦想
Life cycle of Servlet
Monotonic stack -42 Connect rainwater
Parameters of convolutional neural network
求组合数 AcWing 885. 求组合数 I
Analysis of Alibaba canal principle
使用dlv分析golang进程cpu占用高问题
[rust notes] 13 iterator (Part 1)
【Rust 笔记】12-闭包
记忆化搜索 AcWing 901. 滑雪
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令