当前位置:网站首页>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();
}
}
边栏推荐
- 求组合数 AcWing 885. 求组合数 I
- DOM render mount patch responsive system
- Message queue for interprocess communication
- Monotonic stack -84 The largest rectangle in the histogram
- JS non Boolean operation - learning notes
- Common DOS commands
- C language student management system based on linked list, super detailed
- 22-06-27 Xian redis (01) commands for installing five common data types: redis and redis
- [rust notes] 05 error handling
- [rust note] 10 operator overloading
猜你喜欢
随机推荐
Downward compatibility and upward compatibility
LeetCode 324. 摆动排序 II
【Rust 笔记】07-结构体
[concurrent programming] consistency hash
PHP function date (), y-m-d h:i:s in English case
Redux - learning notes
<, < <,>, > > Introduction in shell
Format - C language project sub file
LeetCode 241. 为运算表达式设计优先级
Introduction to the usage of getopts in shell
请求参数的发送和接收
Six dimensional space (C language)
Complex character + number pyramid
[concurrent programming] working mechanism and type of thread pool
LeetCode 871. 最低加油次数
单调栈-84. 柱状图中最大的矩形
Monotonic stack -503 Next bigger Element II
[concurrent programming] explicit lock and AQS
求组合数 AcWing 886. 求组合数 II
[concurrent programming] atomic operation CAS






