八大排序算法 —— 快速排序全揭秘:从基础到优化
创作时间:
作者:
@小白创作中心
八大排序算法 —— 快速排序全揭秘:从基础到优化
引用
CSDN
1.
https://blog.csdn.net/charlie_lee_cs/article/details/139554753
快速排序简介
定义
快速排序:快速排序也采用分治策略,选择一个基准元素,将数组分成比基准小和比基准大的两部分,再对两部分递归地进行排序。快速排序的平均时间复杂度为O(n log n),是目前应用广泛的排序算法之一。
时间复杂度
- 最坏情况:O(n²)
- 平均情况:O(n log₂n)
- 最佳情况:O(n log₂n)
相关资源
- 排序数组 - 力扣(LeetCode)
最优的Partition算法 🔥
Introsort简介
Introsort(内排序)从快速排序开始作为主要排序算法。在最坏情况下(例如,数组已经排序或接近排序),快速排序可能退化为O(n²)时间复杂度。为了避免快速排序的最坏情况,Introsort引入了一个最大递归深度。当递归深度超过这个阈值时,算法切换到堆排序或归并排序,以确保更好的最坏情况性能。
template <typename Tp>
int partition(vector<Tp>& nums, int lIdx, int rIdx) {
int randomIndex = lIdx + rand() % (rIdx - lIdx + 1);
std::swap(nums[randomIndex], nums[rIdx]);
Tp pivot = nums[rIdx];
int lBoundary = lIdx;
int rBoundary = rIdx - 1;
for(; ; ++lBoundary, --rBoundary){
for (; lBoundary <= rBoundary && nums[lBoundary] < pivot; ++lBoundary) {}
for (; lBoundary <= rBoundary && nums[rBoundary] > pivot; --rBoundary) {}
if (lBoundary > rBoundary) {
break;
}
std::swap(nums[lBoundary], nums[rBoundary]);
}
std::swap(nums[rIdx], nums[lBoundary]);
return lBoundary;
}
过程示例
- 假设
nums = [7, 3, 5, 1, 2, 6, 4]
,随机选择的pivot下标为5,即6与最右的4交换,得到
nums = [7, 3, 5, 1, 2, 4, 6]
。 - 分区指针起始如图:
left (lIdx) -> 7, 3, 5, 1, 2, 4 <- right (rIdx), 6(pivot)
。 - 左指针移动到第一个大于或等于主元的元素(即7),右指针移动到第一个小于或等于主元的元素(为4):
left (lIdx) -> 7, 3, 5, 1, 2, 4 <- right (rIdx), 6(pivot)
。 - 交换左右指针处的元素:
left (lIdx) -> 4, 3, 5, 1, 2, 7 <- right (rIdx), 6(pivot)
。 - 继续该过程,直到左右指针相遇:
4, 3, 5, 1, 2 <- right (rIdx), left (lIdx) -> 7, 6(pivot)
。 - 将枢轴元素(当前位于右指针处)与左指针处的元素交换(6和7交换)。
非递归快速排序
实现
template <typename Tp>
void quickSort(vector<Tp>& nums) {
std::stack<std::pair<int, int>> stack;
stack.push(std::make_pair(0, nums.size() - 1));
while (!stack.empty()) {
std::pair<int, int> current = stack.top();
stack.pop();
int lIdx = current.first;
int rIdx = current.second;
if (lIdx < rIdx) {
int boundary = partition(nums, lIdx, rIdx);
stack.push(std::make_pair(lIdx, boundary - 1));
stack.push(std::make_pair(boundary + 1, rIdx));
}
}
}
递归快速排序
实现
template <typename Tp>
void qSortRecursion(vector<Tp>& nums, const int& lIdx, const int& rIdx) {
if (lIdx < rIdx) {
int boundary = partition(nums, lIdx, rIdx);
qSortRecursion(nums, lIdx, boundary - 1);
qSortRecursion(nums, boundary + 1, rIdx);
}
}
template <typename Tp>
void quickSort(vector<Tp>& nums) {
qSortRecursion(nums, 0, nums.size() - 1);
}
有问题的Partition
实现
大量重复元素会超时:
template <typename Tp>
int partition(vector<Tp>& nums, int lIdx, int rIdx) {
// 较为有序时, 避免超时
int randIdx = lIdx + rand() % (rIdx - lIdx + 1);
std::swap(nums[randIdx], nums[rIdx]);
int pivot = nums[rIdx];
int boundary = lIdx;
for (int idx = lIdx; idx < rIdx; ++idx) {
if (nums[idx] < pivot) {
std::swap(nums[idx], nums[boundary]);
++boundary;
}
}
std::swap(nums[boundary], nums[rIdx]); // pivot
return boundary;
}
通过内排序Introsort修复:
template <typename Tp>
void quickSort(vector<Tp>& nums) {
double recThreshold = log10(nums.size()) / log10(2);
int recDepth = 0;
std::stack<std::pair<int, int>> stack;
stack.push(std::make_pair(0, nums.size() - 1));
while (!stack.empty()) {
++recDepth;
if (recDepth >= recThreshold) {
heapSort(nums);
break;
}
std::pair<int, int> current = stack.top();
stack.pop();
int lIdx = current.first;
int rIdx = current.second;
if (lIdx < rIdx) {
int boundary = partition(nums, lIdx, rIdx);
stack.push(std::make_pair(lIdx, boundary - 1));
stack.push(std::make_pair(boundary + 1, rIdx));
}
}
}
三中位数主元选择
实现
template <typename Tp>
int choosePivot(vector<Tp>& nums, int lIdx, int rIdx) {
int mid = lIdx + (rIdx - lIdx) / 2;
if (nums[lIdx] > nums[mid]) {
std::swap(nums[lIdx], nums[mid]);
}
if (nums[mid] > nums[rIdx]) {
std::swap(nums[mid], nums[rIdx]);
}
if (nums[lIdx] > nums[mid]) {
std::swap(nums[lIdx], nums[mid]);
}
return mid;
}
template <typename Tp>
int partition(vector<Tp>& nums, int lIdx, int rIdx) {
int pivotIdx = choosePivot(nums, lIdx, rIdx);
std::swap(nums[pivotIdx], nums[rIdx]);
Tp pivot = nums[rIdx];
int lBoundary = lIdx;
int rBoundary = rIdx - 1;
for(; ; ++lBoundary, --rBoundary){
for (; lBoundary <= rBoundary && nums[lBoundary] < pivot; ++lBoundary) {}
for (; lBoundary <= rBoundary && nums[rBoundary] > pivot; --rBoundary) {}
if (lBoundary > rBoundary) {
break;
}
std::swap(nums[lBoundary], nums[rBoundary]);
}
std::swap(nums[rIdx], nums[lBoundary]);
return lBoundary;
}
总结
快速排序作为一种现代化的排序算法,通过分治策略和递归实现,高效地解决了大多数排序问题。使用最优的Partition算法和三中位数主元选择可以有效优化快速排序的性能,并避免最坏情况的出现。
热门推荐
科技创新“点燃”产业升级新引擎
肺结节不可怕,肺部低剂量螺旋CT扫描来筛查
V8引擎揭秘:汽车高性能的秘密武器
脱发会不会影响个人的自信心和心理健康?
如何判断物业公司的服务质量?这样的判断依据是什么?
易鑫车贷有签买卖合同吗?二手车交易合同签订指南
一张流程图教你时间轴制作方法
脱发与心理健康之间是否存在关联?
提升土地要素配置效率,加快新质生产力形成
above和over的区别是什么 有什么不同用法
如何通过成交量判断市场活跃度?
红烧大肠的营养价值和食用注意事项
运动后这样喝水伤心脏!正确的补水模式是……
老公外遇欲离婚怎样进行婚姻咨询
手机护眼模式真的可以保护眼睛吗?武汉普瑞眼科医院为你解答
车桥战役:一次虎口拔牙的歼灭战
长期喝白开水与喝纯净水的人相比,谁的身体更健康?
别等车出问题才后悔!火花塞更换周期与选择全攻略
如何优化等额本息还款策略?
养花怎么防止小飞虫(几个小技巧摆脱盆土里滋生的小黑飞)
音乐诗人的生命绝唱——方大同
领带颜色和图案的选择有什么讲究?
这种水喝多了有“致癌风险”,喝水注意这3点禁忌!
临时身份证明办理流程:机场补办指南
太阳能辅助采暖设计原理及优点介绍
ASO优化指南:提高APP在应用商店知名度的5大策略
断代史和通史的区别是什么
韩国历任总统个人资料
真空腊肉用放冰箱吗?真空腊肉保质期多长时间
2024年全国高考本科上线率大汇总:四川、河南等30省数据全解析