当前位置:网站首页>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
边栏推荐
- Installation tutorial and simple call of Matplotlib
- RDK仿真实验
- ESP32-C3入门教程 问题篇⑪——esp-tls: create_ssl_handle failed, tls_io_instance->options.trusted_certs null
- Memory mapping of QT
- 使用NPOI导出Excel文件
- Web聊天工具
- Please, stop painting star! This has nothing to do with patriotism!
- 又一所双非改考408,会爆冷么?南昌航空大学软件学院
- QQmlApplicationEngine
- 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
猜你喜欢

NM01-独立于总线协议的NM模块功能概述与API定义

Customize a loading instruction

RDK仿真实验

微信小程序视频分享平台系统毕业设计毕设(6)开题答辩PPT

微信核酸检测预约小程序系统毕业设计毕设(4)开题报告

Relax again! These fresh students can settle directly in Shanghai

Babbitt | metauniverse daily must read: can you buy a virtual anchor for 1000 yuan? Is this the live gospel of small businesses or "cutting leeks"

Leetcode interview question 16.17 Continuous sequence

Qt官方示例:Qt Quick Controls - Gallery

Chrome officially supports MathML, which is enabled in chromium dev 105 by default
随机推荐
Unity学习shader笔记[八十一]简单的颜色调整后处理(亮度,饱和度,对比度)
QQmlApplicationEngine
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
Leetcode interview question 16.15 Abacus wonderful calculation
Simulateur nightGod + application de test de capture de paquets Fiddler
Uncover the whole link communication process of dewu customer service im
Web chat tool
Tower safety monitoring system unattended inclination vibration monitoring system
RDK仿真实验
qt的内存映射
719. Find the distance of the number pair with the smallest K
一款简约PHP个人发卡程序V4.0版本
Leetcode interview question 16.11 Diving board
快速排序基本思路(通俗易懂+例子)「建议收藏」
微信小程序视频分享平台系统毕业设计毕设(3)后台功能
How can you omit a large number of switch statements
Typescript
RTE11- 中断解耦功能
架构设计——ID生成器「建议收藏」
MySQL about only_ full_ group_ By limit