当前位置:网站首页>[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
边栏推荐
- Sentinel 实现 pull 模式规则持久化
- Qt程序最小化托盘后,再弹出个msgbox,点击确定后程序退出问题解决
- How to gracefully realize idempotency and distributed current limiting of distributed interfaces (glory Collection Edition)
- redis 缓存设置,实现类似putIfAbsent功能
- 简单使用 MySQL 索引
- Five best WordPress advertising plug-ins
- 38. REM adaptive layout
- JSON tutorial [easy to understand]
- 零基础学习CANoe Panel(10)—— 复选框(CheckBox)
- 每日三题 7.21
猜你喜欢

563 pages (300000 words) overall design scheme of smart Chemical Park (phase I)

How to build a node development environment efficiently

Sentinel 流量控制快速入门
![[FPGA]: IP core - multiplier](/img/c5/141ba8e5291454bb33225c7e28567d.png)
[FPGA]: IP core - multiplier

【类、抽象与继承】

用 Signal Processing Toolbox 软件对数据进行滤波

数组元素移除问题

How to gracefully realize idempotency and distributed current limiting of distributed interfaces (glory Collection Edition)
![[FPGA]: IP core --divider (divider)](/img/bc/d8b7638e236c468ba23c8afc7ab70e.png)
[FPGA]: IP core --divider (divider)

Google cooperates with colleges and universities to develop a general model, proteogan, which can design and generate proteins with new functions
随机推荐
Detailed explanation of Flink operation architecture
机器学习小试(11)验证码识别测试-使用Qt与Tensorflow2进行深度学习实验
BBR 与 queuing
零基础学习CANoe Panel(7)—— 开关/显示控件(Input/Output Box )
563页(30万字)智慧化工园区(一期)总体设计方案
38. REM adaptive layout
Cookie sessionstorage localstorage differences
Binlog and iptables prevent nmap scanning, xtrabackup full + incremental backup, and the relationship between redlog and binlog
Sentinel implements the persistence of pull pattern rules
Sentinel flow control quick start
Redis cache settings, similar to putifabsent function
How to build a node development environment efficiently
Google cooperates with colleges and universities to develop a general model, proteogan, which can design and generate proteins with new functions
Qt创建应用程序托盘及相关功能
Why should we "go up and down" when testing left shift and right shift?
[dish of learning notes dog learning C] initial level of pointer
[C Primer Plus Chapter 3 after class programming questions]
UVM——双向通信
[dish of learning notes dog learning C] evaluation expression
Hongmeng's first note