当前位置:网站首页>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 applet video sharing platform system graduation design completion (8) graduation design thesis template
- 夜神模拟器+Fiddler抓包测试App
- Typescript
- 铁塔安全监测系统 无人值守倾角振动监测系统
- Wechat applet video sharing platform system graduation design (3) background function
- UE4 用spline畫正圓
- Leetcode 面试题 16.11. 跳水板
- A4988 and 42 stepper motors
- QT official example: QT quick controls - Gallery
- 能解决80%故障的排查思路
猜你喜欢

再放宽!这些应届生,可直接落户上海
![[Yugong series] July 2022 go teaching course 001 introduction to go language premise](/img/f2/3b95f53d67cd1d1979163910dbeeb8.png)
[Yugong series] July 2022 go teaching course 001 introduction to go language premise

MySQL installation and configuration

Chrome officially supports MathML, which is enabled in chromium dev 105 by default

UE4 用spline畫正圓

27:第三章:开发通行证服务:10:【注册/登录】接口:注册/登录OK后,把用户会话信息(uid,utoken)保存到redis和cookie中;(一个主要的点:设置cookie)

好玩的免费GM游戏整理汇总

Leetcode interview question 17.04 Vanishing numbers

又一所双非改考408,会爆冷么?南昌航空大学软件学院

Wechat nucleic acid detection appointment applet system graduation design (2) applet function
随机推荐
Use dosbox to run the assembly super detailed step "suggestions collection"
vi/vim 删除:一行, 一个字符, 单词, 每行第一个字符 命令
好玩的免费GM游戏整理汇总
微信小程序视频分享平台系统毕业设计毕设(4)开题报告
paddlepaddle 28 搭建基于卷积的自动编码机
微信小程序视频分享平台系统毕业设计毕设(7)中期检查报告
Steamos 3.3 beta release, steam deck Chinese keyboard finally came
快速排序基本思路(通俗易懂+例子)「建议收藏」
再放宽!这些应届生,可直接落户上海
719. Find the distance of the number pair with the smallest K
如何优雅的写 Controller 层代码?
微信小程序视频分享平台系统毕业设计毕设(1)开发概要
Clé de cartographie vimium
微信核酸检测预约小程序系统毕业设计毕设(1)开发概要
Vimium mapping key
微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT
MySQL about only_ full_ group_ By limit
Chrome officially supports MathML, which is enabled in chromium dev 105 by default
27:第三章:开发通行证服务:10:【注册/登录】接口:注册/登录OK后,把用户会话信息(uid,utoken)保存到redis和cookie中;(一个主要的点:设置cookie)
微信小程序视频分享平台系统毕业设计毕设(8)毕业设计论文模板