当前位置:网站首页>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
边栏推荐
- UE4 用spline画正圆
- Editor Editor Extension add button and logo in scene view
- 27:第三章:开发通行证服务:10:【注册/登录】接口:注册/登录OK后,把用户会话信息(uid,utoken)保存到redis和cookie中;(一个主要的点:设置cookie)
- Wechat nucleic acid detection and appointment applet system graduation design (3) background function
- cJSON 使用详解
- 微信小程序视频分享平台系统毕业设计毕设(8)毕业设计论文模板
- Leetcode 面试题 17.01. 不用加号的加法
- 微信核酸检测预约小程序系统毕业设计毕设(1)开发概要
- NM02-独立于总线协议的NM模块调用序列图及代码解释
- 1.5.1版本官方docker镜像运行容器,能设置使用 mysql 8驱动吗?
猜你喜欢

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

夜神模拟器+Fiddler抓包测试App

Wechat nucleic acid detection appointment applet system graduation design completion (1) development outline

微信小程序视频分享平台系统毕业设计毕设(3)后台功能

微信小程序视频分享平台系统毕业设计毕设(7)中期检查报告

Summary of fun free GM games

Leetcode 面试题 16.15. 珠玑妙算

NVIDIA graphics card failed to initialize nvml driver/library version mismatch error solution

Uncover the whole link communication process of dewu customer service im

Troubleshooting ideas that can solve 80% of faults
随机推荐
Wechat nucleic acid detection appointment applet system graduation design (2) applet function
Nvidia 显卡 Failed to initialize NVML Driver/library version mismatch 错误解决方案
Qt官方示例:Qt Quick Controls - Gallery
Leetcode 面试题 16.11. 跳水板
Interview, about thread pool
如何开启IDEA的Run Dashboard功能
Leetcode interview question 16.17 Continuous sequence
Web聊天工具
[Yugong series] July 2022 go teaching course 001 introduction to go language premise
A good programmer is worth five ordinary programmers!
Architecture design - ID generator "suggestions collection"
A4988与42步进电机
Wechat nucleic acid detection and appointment applet system graduation design (3) background function
NM01-独立于总线协议的NM模块功能概述与API定义
Unity learning shader notes [81] simple color adjustment post-processing (brightness, saturation, contrast)
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"
Unity学习shader笔记[八十二]增强单通道颜色渲染的黑白处理
Qt官方示例:Qt Quick Controls - Gallery
面试,关于线程池的那些事
win10 卸载cuda