当前位置:网站首页>Basic idea of quick sorting (easy to understand + examples) "suggestions collection"
Basic idea of quick sorting (easy to understand + examples) "suggestions collection"
2022-07-02 18:32:00 【Full stack programmer webmaster】
Hello everyone , I meet you again , I'm your friend, Quan Jun .
Quick sort
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = Today, I saw a quick sorting article written by the great God Blog , awe , I think it's so simple to sort quickly Let's have a simple try
The basic idea of quick sorting is
1、 Take a number from the sequence as the reference number first
2、 Partition process , Put all the larger numbers to the right of it , Less than or equal to its number all put it to the left
3、 Repeat the second step for the left and right sections , Until there is only one number in each interval
To sum up, it is Number of pits to be filled + Divide and conquer method
Here are some examples , There are three main parameters ,i Is the starting address of the interval ,j Is the end address of the interval ,X Is the current starting value
First step ,i=0,j=9,X=21
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
21 | 32 | 43 | 98 | 54 | 45 | 23 | 4 | 66 | 86 |
The second step , from j Start with , Look back and forward , Find the ratio X The small first number a[7]=4, here i=0,j=6,X=21 Replace
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
4 | 32 | 43 | 98 | 54 | 45 | 23 | 21 | 66 | 86 |
The third step , Look from front to back , Find the ratio X The first big number a[1]=32, here i=2,j=6,X=21
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
4 | 21 | 43 | 98 | 54 | 45 | 23 | 32 | 66 | 86 |
Step four , from j=6 Start with , From the back to the front , Find the ratio X The small first number a[0]=4, here i=2,j=0,X=21, Find out j<=i, So the first round ends
You can find 21 The previous numbers are better than 21 Small , The following numbers are better than 21 Big Next, for two subintervals [0,0] and [2,9] Repeat the above operation
The process is given directly below , I'll explain it in detail
i=2,j=6,X=43
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
4 | 21 | 43 | 98 | 54 | 45 | 23 | 32 | 66 | 86 |
i=4,j=6,X=43
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
4 | 21 | 32 | 98 | 54 | 45 | 23 | 43 | 66 | 86 |
i=4,j=5,x=43
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
4 | 21 | 32 | 43 | 54 | 45 | 23 | 98 | 66 | 86 |
i=5,j=5,x=43
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
4 | 21 | 32 | 23 | 43 | 45 | 54 | 98 | 66 | 86 |
Then it is divided into two sub intervals [2,3] and [5,9]
… The final order is the final answer
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 |
---|---|---|---|---|---|---|---|---|---|
4 | 21 | 23 | 32 | 43 | 45 | 54 | 66 | 86 | 98 |
summary :
1.i =L; j = R; The first pit will be excavated a[i].
2.j– Find a smaller number from back to front , After finding it, dig out this number and fill the previous pit a[i] in .
3.i++ Find a larger number from front to back , After finding it, dig out this number and fill it in the previous pit a[j] in .
4. And repeat 2,3 Two steps , until i==j, Fill the reference number in a[i] in .
Code
class Solution {
public void sort(int left,int right,int[] array) {
if (left>=right) return ;
int start=left;
int end=right;
int flag=left;
while(left<right) {
while ((left<right)&&(array[right]>=array[flag])) {
right--;
}
if (array[right]<array[flag]) {
int tmp=array[right];
array[right]=array[flag];
array[flag]=tmp;
flag=right;
}
while ((left<right)&&(array[left]<=array[flag])) {
left++;
}
if (array[left]>array[flag]) {
int tmp=array[left];
array[left]=array[flag];
array[flag]=tmp;
flag=left;
}
}
sort(start, left-1, array);
sort(left+1, end, array);
}
}
Share an awesome learning java Website , Basic knowledge and architecture , Connect as follows : http://how2j.cn?p=54321
Publisher : Full stack programmer stack length , Reprint please indicate the source :https://javaforall.cn/148426.html Link to the original text :https://javaforall.cn
边栏推荐
- Wechat nucleic acid detection appointment applet system graduation design completion (4) opening report
- 使用NPOI导出Excel文件
- 服务器php环境搭建教程,PHP服务端环境搭建图文详解
- QT official example: QT quick controls - Gallery
- cJSON 使用详解
- UE4 用spline畫正圓
- Détends - toi encore! Ces nouveaux étudiants peuvent s'installer directement à Shanghai
- Iframe nesting details
- Use dosbox to run the assembly super detailed step "suggestions collection"
- 2020 Internet industry terminology
猜你喜欢
300+篇文献!一文详解基于Transformer的多模态学习最新进展
Enter a valid user name and password in the Microsoft LDAP configuration page, and enter a valid user name in the Microsoft LDAP configuration page
MySQL installation and configuration
Pychar modify pep8 e501 line too long > 0 characters
实施阴影介绍
Unity学习shader笔记[八十二]增强单通道颜色渲染的黑白处理
Redis(6)----对象与数据结构
Wechat applet video sharing platform system graduation design completion (1) development outline
Wechat applet video sharing platform system graduation design (2) applet function
Implementation shadow introduction
随机推荐
再放寬!這些應届生,可直接落戶上海
【Oracle 期末复习】表空间、表、约束、索引、视图的增删改
Leetcode 面试题 17.04. 消失的数字
C # detect whether the picture is rotated and modified to the true rotation
微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
微信核酸检测预约小程序系统毕业设计毕设(1)开发概要
Chrome 正式支持 MathML,默认在 Chromium Dev 105 中启用
如何开启IDEA的Run Dashboard功能
Uncover the whole link communication process of dewu customer service im
Leetcode interview question 17.01 Addition without plus sign
UE4 draw a circle with spline
Enter a valid user name and password in the Microsoft LDAP configuration page, and enter a valid user name in the Microsoft LDAP configuration page
微信小程序视频分享平台系统毕业设计毕设(2)小程序功能
Meal card hdu2546
【西北工业大学】考研初试复试资料分享
1.5.1版本官方docker镜像运行容器,能设置使用 mysql 8驱动吗?
The official docker image running container in version 1.5.1 can be set to use MySQL 8 driver?
面试,关于线程池的那些事
Export Excel files using npoi
300+ documents! This article explains the latest progress of multimodal learning based on transformer