当前位置:网站首页>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();
}
}
边栏推荐
- How to delete CSDN after sending a wrong blog? How to operate quickly
- Gaussian elimination acwing 883 Gauss elimination for solving linear equations
- 22-06-28 Xi'an redis (02) persistence mechanism, entry, transaction control, master-slave replication mechanism
- 20220630 learning clock in
- Noip 2002 popularity group selection number
- Complex character + number pyramid
- [concurrent programming] collaboration between threads
- MySQL index types B-tree and hash
- 请求参数的发送和接收
- [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
猜你喜欢

PHP uses foreach to get a value in a two-dimensional associative array (with instances)

Gaussian elimination acwing 883 Gauss elimination for solving linear equations

Try to reprint an article about CSDN reprint

Find the combination number acwing 885 Find the combination number I

单调栈-42. 接雨水

Markdown learning

How to use Jupiter notebook

求组合数 AcWing 886. 求组合数 II

Markdown learning

Character pyramid
随机推荐
高斯消元 AcWing 883. 高斯消元解线性方程组
[concurrent programming] synchronization container, concurrent container, blocking queue, double ended queue and work secret
22-06-27 西安 redis(01) 安装redis、redis5种常见数据类型的命令
[rust notes] 06 package and module
MySQL index types B-tree and hash
Methods of checking ports according to processes and checking processes according to ports
Parameters of convolutional neural network
[concurrent programming] explicit lock and AQS
Monotonic stack -84 The largest rectangle in the histogram
[rust notes] 13 iterator (Part 1)
Get the link behind? Parameter value after question mark
Using DLV to analyze the high CPU consumption of golang process
Format - C language project sub file
Convert video to GIF
状态压缩DP AcWing 291. 蒙德里安的梦想
How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework
Unity notes 1
PIC16F648A-E/SS PIC16 8位 微控制器,7KB(4Kx14)
createjs easeljs
我们有个共同的名字,XX工