当前位置:网站首页>快速排序的简单理解
快速排序的简单理解
2022-06-23 14:22:00 【pythonxxoo】
优质资源分享
| 学习路线指引(点击解锁) | 知识定位 | 人群定位 |
|---|---|---|
| 🧡 Python实战微信订餐小程序 🧡 | 进阶级 | 本课程是python flask+微信小程序的完美结合,从项目搭建到腾讯云部署上线,打造一个全栈订餐系统。 |
| Python量化交易实战 | 入门级 | 手把手带你打造一个易扩展、更安全、效率更高的量化交易系统 |
详细描述
快速排序通过一趟排序将待排序列分割成独立的两部分,其中一部分序列的关键字均比另一部分序列的关键字小,则可分别对这两部分序列继续进行排序,以达到整个序列有序的目的。
快速排序详细的执行步骤如下:
- 从序列中挑出一个元素,称为 “基准”(pivot);
- 重新排序序列,所有比基准值小的元素摆放在基准前面,所有比基准值大的元素摆在基准的后面(相同的数可以到任一边)。在这个分区退出之后,该基准就处于序列的中间位置。这个称为分区(partition)操作;
- 递归地(recursive)把小于基准值元素的子序列和大于基准值元素的子序列排序。
算法图解

问题解疑
快速排序可以怎样选择基准值?
第一种方式:固定位置选择基准值;在整个序列已经趋于有序的情况下,效率很低。
第二种方式:随机选取待排序列中任意一个数作为基准值;当该序列趋于有序时,能够让效率提高,但在整个序列数全部相等的时候,随机快排的效率依然很低。
第三种方式:从区间的首、尾、中间,分别取出一个数,然后对比大小,取这 3 个数的中间值作为基准值;这种方式解决了很多特殊的问题,但对于有很多重复值的序列,效果依然不好。
快速排序有什么好的优化方法?
首先,合理选择基准值,将固定位置选择基准值改成三点取中法,可以解决很多特殊的情况,实现更快地分区。
其次,当待排序序列的长度分割到一定大小后,使用插入排序。对于待排序的序列长度很小或基本趋于有序时,插入排序的效率更好。
在排序后,可以将与基准值相等的数放在一起,在下次分割时可以不考虑这些数。对于解决重复数据较多的情况非常有用。
在实现上,递归实现的快速排序在函数尾部有两次递归操作,可以对其使用尾递归优化(简单地说,就是尾位置调用自身)。
代码实现
| | package cn.fatedeity.algorithm.sort; |
| | |
| | import java.util.Random; |
| | |
| | /** |
| | * 快速排序算法 |
| | */ |
| | 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; |
| | } |
| | // 随机数取基准值 |
| | 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); |
| | } |
| | } |
边栏推荐
- Uniswap 收购 NFT交易聚合器 Genie,NFT 交易市场将生变局?
- Error creating bean with name xxx Factory method ‘sqlSessionFactory‘ threw exception; nested excepti
- 信贷产品额度定价场景下的回归模型效果评估
- 2021-05-22
- 2021-05-08
- RF Analyzer Demo搭建
- How to merge tables when exporting excel tables with xlsx
- Cause analysis and intelligent solution of information system row lock waiting
- ASP. Net C pharmacy management information system (including thesis) graduation project [demonstration video]
- Test article
猜你喜欢
![[deeply understand tcapulusdb technology] tcapulusdb import data](/img/c5/fe0c9333b46c25be15ed4ba42f7bf8.png)
[deeply understand tcapulusdb technology] tcapulusdb import data

Tencent ECS failed to send email

【深入理解TcaplusDB技术】TcaplusDB业务数据备份

【二级等保】过二级等保用哪个堡垒机品牌好?

KDD'22「阿里」推荐系统中的通用序列表征学习

如何使用笔记软件 FlowUs、Notion 进行间隔重复?基于公式模版

k8s--部署单机版MySQL,并持久化

腾讯云服务器发送邮件失败

Uniswap 收购 NFT交易聚合器 Genie,NFT 交易市场将生变局?

首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
随机推荐
力扣解法汇总513-找树左下角的值
港股今年最大IPO来了,660亿身家,坐在矿山上的“大王”
加快 yarn install 的三个简单技巧
SAP inventory gain / loss movement type 701 & 702 vs 711 & 712
Effect evaluation of regression model under credit product quota pricing scenario
2021-05-22
Working for 7 years to develop my brother's career transition test: only by running hard can you get what you want~
乐高宣布涨价,炒家更嗨皮了
RF Analyzer Demo搭建
详解Redis分布式锁的原理与实现
Quick view of wechat applet development process
As a software testing practitioner, do you understand your development direction?
HCIA network foundation
Mysql数据库---日志管理、备份与恢复
Uniswap acquires genie, an NFT transaction aggregator. Will the NFT transaction market change?
【二级等保】过二级等保用哪个堡垒机品牌好?
2021-06-07
The second Tencent light · public welfare innovation challenge was launched, and the three competition topics focused on the social value of sustainable development
【深入理解TcaplusDB技术】TcaplusDB业务数据备份
[in depth understanding of tcapulusdb technology] tcapulusdb construction data