八大排序算法 —— 快速排序全揭秘:从基础到优化
创作时间:
作者:
@小白创作中心
八大排序算法 —— 快速排序全揭秘:从基础到优化
引用
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算法和三中位数主元选择可以有效优化快速排序的性能,并避免最坏情况的出现。
热门推荐
如何通过饮食和生活方式预防中风、脑梗死、脑出血?
形式主义对现代社会发展的影响与反思
围棋入门:从基本规则到胜负判定
最严院士增选,27名候选人被处理,或被永久取消院士参选资格
建筑百科:强力瓷砖胶的正确使用与安装技巧
阿托伐他汀与瑞舒伐他汀,哪个更好更安全?该怎么选?药师讲清楚
为什么那么多人喜欢去泰国旅游?
亚马逊裁员:战略调整与市场挑战
人形机器人:政策助力下的技术与市场双突破
西游记里,四大最强妖怪排行榜,第一法力高强肉身成圣
上海交大发布蛋白质设计模型“Venus”
同价位显卡选购指南:性能与性价比的终极平衡
人工智能助力直播电商发展的路径研究
发展现代设施农业,如何提高温室大棚作物产量?
八字命理研究官方网站:如何科学解读个人命运
欧洲37城旅游性价比大排名:维尔纽斯成最大"性价比之王"
清初四大贝勒:历史与传承
如何规避二套房的购买风险?二套房的购买有哪些注意事项?
我国早期青铜乐器的传承与发展
为什么说50mm是人像黄金镜头呢!
有效利用控制图提升生产质量管理技巧
重点实验室与清华大学合作揭示植物感知渗透胁迫的新机制
黄金的象征着什么?黄金的象征意义在投资中有何体现?
闰月是什么 闰月是怎么算的
手机家电等产品补贴效果显著,消费活力持续释放
石蜡切片是什么
教育不止于课堂:用一场公益活动,为这个春天增添动人的注脚
指南:遭遇网暴,如何自救
成为自由职业者后,他们更“自由”了吗?
2024-25赛季NBA全明星赛制规则详解:四队参赛,一战定胜负