当前位置:网站首页>[interview: Basics 05: quick sort]
[interview: Basics 05: quick sort]
2022-07-24 10:48:00 【I cream】
【 interview : The basic chapter 05: Quick sort 】
00. Preface
Please point out any questions , thank .
01. brief introduction
Quick sort is an advanced sort algorithm , The average time complexity can reach O(n l o g 2 n log_2n log2n), Its main idea is to find a datum point larger than the datum point and put it at one end of the datum point Those smaller than the reference point shall be placed at one end of the reference point Repeat the process at each end Implement with recursion .
General idea : The reference point of each trip remains unchanged Finally, exchange benchmarks
There are two mainstream methods to realize quick sorting :
1. Unilateral circulation method : Select the rightmost end as the benchmark , There are two variables i j,i Used to maintain the boundary of elements smaller than the datum point , That is, the target index of each exchange , j Responsible for finding elements smaller than the benchmark , Once found with i swapping , Last i Exchange with the reference point Complete a sequence .
2. Bilateral circular law : Select the leftmost end as the benchmark , There are two variables i j,i Responsible for finding elements larger than the benchmark from left to right , j Responsible for finding elements smaller than the benchmark , Once found j And i swapping , Finally, the benchmark is connected with i j Switching of coincidence points A sort is done .
02. Algorithm steps
Pair sequence {5,3,7,2,9,8,1,4} Perform ascending quick sort .
Unilateral circulation method ( The rightmost end is the reference point ):
i=0 And j=1 swapping i=1 And j=3 swapping i=2 And j=6 swapping i=3 And r=7 swapping
3 2 1 4 9 8 7 5
i=0 And r=2 swapping
1 2 3 4 9 8 7 5
i=1 And j=1 swapping i=2 And r=2 swapping
1 2 3 4 9 8 7 5
i=4 And r=7 swapping
1 2 3 4 5 8 7 9
i=5 And j=5 swapping i=6 And j=6 swapping i=7 And r=7 swapping
1 2 3 4 5 8 7 9
i=5 And r=6 swapping
1 2 3 4 5 7 8 9
Bilateral circular law ( The leftmost end is the reference point ):
Datum subscript 0 Reference point value 5
i=2 And j=7 swapping i=4 And j=6 swapping i=4 And j=4 swapping l=0 And j=4 swapping
1 3 4 2 5 8 9 7
Datum subscript 0 Reference point value 1
i=0 And j=0 swapping l=0 And j=0 swapping
1 3 4 2 5 8 9 7
Datum subscript 1 Reference point value 3
i=2 And j=3 swapping i=2 And j=2 swapping l=1 And j=2 swapping
1 2 3 4 5 8 9 7
Datum subscript 5 Reference point value 8
i=6 And j=7 swapping i=6 And j=6 swapping l=5 And j=6 swapping
1 2 3 4 5 7 8 9
Several problems that should be paid attention to in the bilateral circulation method
1. The reference point is on the left And first j-- Again i++
2.while(i<j) Be sure to add. Otherwise, the sorted values will be scrambled again
3.while(i<j&&a[i]<=x) = Be sure to add. To prevent the datum point from being moved
Code implementation
Unilateral circulation method :
public static void sort1(int[] a,int l,int r){
if (l>=r)
return;
int i=l;
int x = a[r];
for (int j=l;j<r;j++){
if (a[j]<x){
int t=a[j];
a[j]=a[i];
a[i]=t;
i++;
}
}
int t=a[r];
a[r]=a[i];
a[i]=t;
for (int k=0;k<a.length;k++)
System.out.printf("%d ",a[k]);
System.out.println();
sort2(a,l,i-1);
sort2(a,i+1,r);
}
public static void main(String[] args) {
int[] a = {
5,3,7,2,9,8,1,4};
sort1(a,0,a.length-1);
}
result
3 2 1 4 9 8 7 5
1 2 3 4 9 8 7 5
1 2 3 4 9 8 7 5
1 2 3 4 5 8 7 9
1 2 3 4 5 8 7 9
1 2 3 4 5 7 8 9
Bilateral circular law
public static void sort2(int[] a,int l,int r){
if (l>=r)
return;
int x = a[l];
int i = l;
int j = r;
while (i<j){
while (i<j&&a[j]>x)// The order should be fixed , If not fixed, it will lead to i Find the last value greater than the reference point and stop j because
// Want to find small Then finally with i coincidence Exit loop But now j No value smaller than the reference point has been found So finally
// Errors will be caused by the final exchange with the benchmark
j--;
while (i<j&&a[i]<=x)// = In order to prevent The reference point is moved ,i<j To prevent the sorted values from being disturbed again
i++;
int t=a[i];
a[i]=a[j];
a[j]=t;
}
int t=a[l];
a[l]=a[j];
a[j]=t;
for (int k=0;k<a.length;k++)
System.out.printf("%d ",a[k]);
System.out.println();
sort1(a,l,j-1);
sort1(a,j+1,r);
}
public static void main(String[] args) {
int[] a = {
5,3,7,2,9,8,1,4};
sort2(a,0,a.length-1);
}
result
1 3 4 2 5 8 9 7
1 3 4 2 5 8 9 7
1 2 3 4 5 8 9 7
1 2 3 4 5 7 8 9
边栏推荐
猜你喜欢

机器学习小试(11)验证码识别测试-使用Qt与Tensorflow2进行深度学习实验

Distributed transaction processing scheme big PK!

Volcanic engine: open ByteDance, the same AI infrastructure, a system to solve multiple training tasks

Zero basic learning canoe panel (6) -- switch/indicator
![[personal summary] end of July 17, 2022](/img/56/8c69b171140ca38e16f0bbb7f344e3.jpg)
[personal summary] end of July 17, 2022

Arduino + AD9833 波形发生器

Overview of basic knowledge of binary tree

【微服务】Eureka+Ribbon实现注册中心与负载均衡

Zero basic learning canoe panel (3) -- static text, group box, picture box

ECCV 2022 | 清华提出首个嵌入光谱稀疏性的Transformer
随机推荐
Sentinel flow control quick start
零基础学习CANoe Panel(10)—— 复选框(CheckBox)
Qt程序最小化托盘后,再弹出个msgbox,点击确定后程序退出问题解决
[class, abstraction and inheritance]
IEPE vibration sensor synchronous signal acquisition card /icp synchronous data network acquisition module
Machine learning quiz (10) using QT and tensorflow to create cnn/fnn test environment
I admire a Google boss very much, and he left..
UVM——双向通信
How to gracefully realize idempotency and distributed current limiting of distributed interfaces (glory Collection Edition)
Protocol Bible - talk about ports and quads
When to use obj['attribute name'] for the attribute name of an object
Google cooperates with colleges and universities to develop a general model, proteogan, which can design and generate proteins with new functions
[dish of learning notes dog learning C] evaluation expression
[FPGA]: IP core DDS
【微服务】Eureka+Ribbon实现注册中心与负载均衡
Activity review | Anyuan AI X machine heart series lecture No. 1 | deepmind research scientist rohin Shah shares "finding a safe path for AgI"
协议圣经-谈端口和四元组
简单使用 MySQL 索引
[FPGA]: frequency measurement
PC博物馆(2) 1972年 HP-9830A