当前位置:网站首页>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();
}
}
边栏推荐
- On the setting of global variable position in C language
- Final review of Database Principles
- Using DLV to analyze the high CPU consumption of golang process
- Low code momentum, this information management system development artifact, you deserve it!
- [rust notes] 07 structure
- String splicing method in shell
- JS non Boolean operation - learning notes
- Slice and index of array with data type
- DOM render mount patch responsive system
- [rust notes] 06 package and module
猜你喜欢
![[concurrent programming] thread foundation and sharing between threads](/img/26/60fbfe65b186867a3b1cb58d481226.jpg)
[concurrent programming] thread foundation and sharing between threads

AcWing 787. 归并排序(模板)

浅谈企业信息化建设

MySQL three logs

TP5 multi condition sorting

LeetCode 535. TinyURL 的加密与解密

DOM 渲染系统(render mount patch)响应式系统

Mortgage Calculator

How to place the parameters of the controller in the view after encountering the input textarea tag in the TP framework

AcWing 785. 快速排序(模板)
随机推荐
Methods of using arrays as function parameters in shell
[concurrent programming] working mechanism and type of thread pool
Really explain the five data structures of redis
The difference between if -n and -z in shell
Common DOS commands
22-05-26 Xi'an interview question (01) preparation
Deep parsing (picture and text) JVM garbage collector (II)
Alibaba canal actual combat
C language student management system based on linked list, super detailed
记忆化搜索 AcWing 901. 滑雪
SQL statement error of common bug caused by Excel cell content that is not paid attention to for a long time
Dom4j traverses and updates XML
拯救剧荒,程序员最爱看的高分美剧TOP10
分配异常的servlet
Gaussian elimination acwing 883 Gauss elimination for solving linear equations
[concurrent programming] collaboration between threads
How to delete CSDN after sending a wrong blog? How to operate quickly
Binary tree sorting (C language, char type)
Solution of 300ms delay of mobile phone
[rust notes] 13 iterator (Part 1)