当前位置:网站首页>Simple understanding of quick sort
Simple understanding of quick sort
2022-06-23 15:03:00 【pythonxxoo】
High quality resource sharing
| Learning route guidance ( Click unlock ) | Knowledge orientation | Crowd positioning |
|---|---|---|
| 🧡 Python Actual wechat ordering applet 🧡 | Progressive class | This course is python flask+ Perfect combination of wechat applet , From the deployment of Tencent to the launch of the project , Create a full stack ordering system . |
| Python Quantitative trading practice | beginner | Take you hand in hand to create an easy to expand 、 More secure 、 More efficient quantitative trading system |
Detailed description
Quick sort divides the sequence to be arranged into two independent parts through one-time sorting , The keywords of some sequences are smaller than those of other sequences , Then the two parts of the sequence can be sorted separately , In order to achieve the purpose of orderly sequence .
The detailed implementation steps of quick sort are as follows :
- Pick an element from the sequence , be called “ The benchmark ”(pivot);
- Reorder the sequence , All elements smaller than the benchmark value are placed in front of the benchmark , All elements larger than the benchmark value are placed behind the benchmark ( The same number can go to either side ). After the partition exits , The benchmark is in the middle of the sequence . This is called a partition (partition) operation ;
- recursively (recursive) Sort the subsequence of elements smaller than the reference value and subsequence of elements larger than the reference value .
Algorithm diagram

Problem solving
How to select a benchmark for quick sort ?
The first way : Fixed position selection reference value ; When the whole sequence has become orderly , Efficiency is very low .
The second way : Randomly select any number in the sequence to be arranged as the reference value ; When the sequence tends to order , Can improve efficiency , But when the whole sequence is all equal , The efficiency of random fast platoon is still very low .
The third way : From the beginning of the interval 、 tail 、 middle , Take out a number , And then compare the size , Take this 3 The middle value of the number is used as the reference value ; This method solves many special problems , But for a sequence with many duplicate values , The effect is still not good .
What is a good optimization method for quick sorting ?
First , Reasonably select the reference value , Change the fixed position selection reference value to the three-point centring method , It can solve many special situations , Achieve faster partitioning .
secondly , When the length of the sequence to be sorted is divided to a certain size , Use insert sort . When the length of the sequence to be sorted is very small or basically tends to be orderly , Insert sort is more efficient .
After sorting , Numbers equal to the reference value can be put together , These numbers can be ignored in the next split . It is very useful to solve the problem of large amount of duplicate data .
On the implementation , Recursive quick sort has two recursive operations at the end of the function , Tail recursion optimization can be used for it ( In short , That is, the tail position calls itself ).
Code implementation
| | package cn.fatedeity.algorithm.sort; |
| | |
| | import java.util.Random; |
| | |
| | /** |
| | * Fast sorting algorithm |
| | */ |
| | public class QuickSort { |
| | private static void swap(int[] numbers, int src, int target) { |
| | int temp = numbers[src]; |
| | numbers[src] = numbers[target]; |
| | numbers[target] = temp; |
| | } |
| | |
| | private static int[] sort(int[] numbers, int low, int high) { |
| | if (low > high) { |
| | return numbers; |
| | } |
| | // Reference value of random number |
| | Random random = new Random(); |
| | int pivotIndex = random.nextInt(low, high + 1); |
| | int pivot = numbers[pivotIndex]; |
| | swap(numbers, pivotIndex, low); |
| | |
| | int mid = low + 1; |
| | for (int i = low + 1; i <= high; i++) { |
| | if (numbers[i] < pivot) { |
| | swap(numbers, i, mid); |
| | mid++; |
| | } |
| | } |
| | swap(numbers, low, --mid); |
| | sort(numbers, low, mid - 1); |
| | sort(numbers, mid + 1, high); |
| | return numbers; |
| | } |
| | |
| | public static int[] sort(int[] numbers) { |
| | return sort(numbers, 0, numbers.length - 1); |
| | } |
| | } |
边栏推荐
- [cloud based co creation] intelligent supply chain plan: improve the decision-making level of the supply chain and help enterprises reduce costs and increase efficiency
- High quality coding - air quality map visualization
- 大厂架构师:如何画一张大气的业务大图?
- 从3开始,在业务系统中增加分页功能
- Qu'est - ce que ça veut dire? Où dois - je m'inscrire?
- Uniswap acquires genie, an NFT transaction aggregator. Will the NFT transaction market change?
- Error 1079 when starting a service: the account of this service is different from that of other services running on the same process
- golang--判断字符串是否相等
- ai智能机器人让我们工作省时省力
- [datahub] LinkedIn datahub learning notes
猜你喜欢

Why is Xiaomi stuck in the chip quagmire?

百万奖金等你来拿,首届中国元宇宙创新应用大赛联合创业黑马火热招募中!

Un million de bonus vous attend, le premier concours d'innovation et d'application de la Chine Yuan cosmique Joint Venture Black Horse Hot Recruitment!
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/c5/fe0c9333b46c25be15ed4ba42f7bf8.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

小米为何深陷芯片泥潭?
Xampp中mysql无法启动问题的解决方法

乐高宣布涨价,炒家更嗨皮了

杀入美团、饿了么腹地,京东外卖劲儿有多大?

In this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3

【opencv450】椒盐噪声demo
随机推荐
如何解决 Iterative 半监督训练 在 ASR 训练中难以落地的问题丨RTC Dev Meetup
2021-05-08
golang--文件的多个处理场景
Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
In this year's English college entrance examination, CMU delivered 134 high scores with reconstruction pre training, significantly surpassing gpt3
期货怎么开户安全吗,期货手续费哪家期货公司比较低,适合散户开户?
idea查看.class文件 idea查看.class文件夹
力扣解法汇总513-找树左下角的值
【二级等保】过二级等保用哪个堡垒机品牌好?
LEGO announces price increase, speculators are more excited
《墨者学院——SQL手工注入漏洞测试(MySQL数据库)》
2021-05-08
小米为何深陷芯片泥潭?
直播间源码在开发前期必须做的工作及开发步骤
Mysql双主配置的详细步骤
js遍历数组(用forEach()方法)
阿里 Seata 新版本终于解决了 TCC 模式的幂等、悬挂和空回滚问题
去 OPPO 面试, 被问麻了。。。
golang--判断字符串是否相等
这届文娱人,将副业做成了主业