当前位置: 首页 > news >正文

C++经典排序技巧总结

C++作为一门高效的编程语言,其标准库提供了强大的排序功能

1. STL Sort函数

标准模板库(STL)中的 sort()函数是最常用的排序方法,它基于快速排序算法,但是会根据元素数量和数据分布适应性地采用插入排序或堆排序。

#include <algorithm>
#include <vector>std::vector<int> nums = {4, 1, 3, 5, 2};
std::sort(nums.begin(), nums.end());

2. 自定义比较函数

可以自定义比较函数来实现特定的排序规则,此比较函数必须是一个返回bool类型的二元谓词。

#include <algorithm>
#include <vector>bool compare(int a, int b) {return a > b; // 降序排列
}std::vector<int> nums = {4, 1, 3, 5, 2};
std::sort(nums.begin(), nums.end(), compare);

3. 使用Lambda表达式

C++11引入的Lambda表达式可以让你在调用 sort()时直接写内联比较逻辑,使代码更加紧凑。

#include <algorithm>
#include <vector>std::vector<int> nums = {4, 1, 3, 5, 2};
std::sort(nums.begin(), nums.end(), [](int a, int b) {return a < b; // 升序排列
});

4. 部分排序

STL中的 partial_sort可以对集合的一部分元素进行排序,其余元素未必保证有序。

#include <algorithm>
#include <vector>std::vector<int> nums = {4, 1, 3, 5, 2};
std::partial_sort(nums.begin(), nums.begin() + 3, nums.end());

5. 稳定排序

stable_sort类似于 sort,区别在于它保持相等元素的相对顺序。这适用于保持多个字段的顺序排序。

#include <algorithm>
#include <vector>std::vector<std::pair<int, char>> pairs = {{1, 'A'}, {2, 'B'}, {1, 'B'}, {2, 'A'}};
std::stable_sort(pairs.begin(), pairs.end());

6. nth_element的使用

当你需要找到经过排序后会位于第n个位置的元素,而不需要对整个集合排序时,可以使用 nth_element

#include <algorithm>
#include <vector>std::vector<int> nums = {4, 1, 3, 5, 2};
std::nth_element(nums.begin(), nums.begin() + 2, nums.end());

7. 堆排序

使用STL中的 make_heap()push_heap()pop_heap()可以实现堆排序,适用于动态数据集合的高效排序。

#include <algorithm>
#include <vector>std::vector<int> nums = {4, 1, 3, 5, 2};
std::make_heap(nums.begin(), nums.end());
std::sort_heap(nums.begin(), nums.end());

8. 插入排序

对于小型数据集或几乎排序好的数据集,插入排序是一个很好的选择,因为它有很低的开销。

#include <vector>std::vector<int> nums = {4, 1, 3, 5, 2};
for (int i = 1; i < nums.size(); ++i) {int key = nums[i];int j = i - 1;while (j >= 0 && nums[j] > key) {nums[j + 1] = nums[j];j = j - 1;}nums[j + 1] = key;
}

9. 快速排序

自己实现快速排序算法,可以更好地理解其分而治之的原理。

#include <vector>int partition(std::vector<int>& nums, int low, int high) {int pivot = nums[high];int i = (low - 1);for (int j = low; j <= high - 1; j++) {if (nums[j] < pivot) {i++;std::swap(nums[i], nums[j]);}}std::swap(nums[i + 1], nums[high]);return (i + 1);
}void quickSort(std::vector<int>& nums, int low, int high) {if (low < high) {int pi = partition(nums, low, high);quickSort(nums, low, pi - 1);quickSort(nums, pi + 1, high);}
}
http://www.hskmm.com/?act=detail&tid=11980

相关文章:

  • 静态资源管理:Nginx在Docker中的部署
  • C#文件操作入门
  • javascript基础 - Ref
  • ES——(一)基本概念 - 指南
  • python2.7+pandas
  • SAP集成HTTP接口(x-www-form-urlencoded格式)
  • iText与OpenPDF使用差异及中文处理完全指南 - 实践
  • 图解17:5中网络IO模型
  • Fmt库在CentOS 7的应用指南
  • 在k8s集群中解决master节点与node通信
  • 在Go中构建应用级IP防火墙机制
  • 用 R 语言实现验证码识别
  • 用 Lua 实现验证码识别
  • PHP中常见数组操作函数
  • AI翻唱神器,一键用你喜欢的歌手翻唱他人的曲目(附下载链接)
  • 修复Ubuntu系统文件损坏:手动fsck指令
  • Python网络请求库requests使用详述
  • Composer在PHP项目中的手动类自动加载策略
  • window表现驱动开发—视频呈现网络简介
  • 一类特征方程在数列递推中的应用
  • rust跨文件调用代码
  • 详细介绍:导师推荐毕设:基于SpringBoot+Vue的中小企业进销存管理系统设计
  • NIO重构UDP收发模块
  • nvidia-smi 卡死问题解决
  • 临时
  • 题解:SP6562 PRUBALL - Esferas
  • 个人项目-文本查重
  • CSPS 2025游记
  • CMake 常用语句
  • 电脑硬件温度、占用率实时监控软件