InsertionSort(插入排序)原理及C++代码实现
2024-09-02 22:12:39
插入排序是最常用的排序之一。
在输入规模较小的时候,插入排序的性能较好。
最好情况下插入排序的时间复杂度是O(n),平均情况则为O(n2)。
插入排序是稳定的排序算法之一。
基本思路为从第二个元素开始,依次插入前面已经排好序的序列,利用循环不变式很容易理解。
代码如下:(仅供参考)
void InsertionSort(int * const begin, int * const end) {
int i, j;
int key;
for (i = ; i < end - begin; ++i) {
key = *(begin + i);
for (j = i - ; j >= && (*(begin + j) > key); --j) {
*(begin + j + ) = *(begin + j);
}
*(begin + j + ) = key;
}
}
注:指针需要支持随机访问
最新文章
- CozyRSS开发记录18-番外之Atom1.0的支持
- easyui DataGrid 工具类之 util js
- [知识点]Trie树和AC自动机
- linux注销开关机
- win8上安装 Pillow
- 设计模式之前奏(UML类图)
- Ehcache 的简单实用 及配置
- 移动端车牌识别、行驶证识别OCR为共享汽车APP增添技术色彩
- Docker 单主机网络
- PCA,到底在做什么
- heartbeat 非联网安装(通过配置本地yum文件库安装heartbeat)
- Modbus库开发笔记之二:Modbus消息帧的生成
- git push以后GitHub上文件夹灰色 不可点击
- ubuntu-14.04.2-desktop-i386.iso:ubuntu-14.04.2-desktop-i386:安装Oracle11gR2
- Codeforces 691C. Exponential notation 模拟题
- jquery-插入兄弟元素
- Hibernate 干货
- ActionContextCleanUp
- Hibernate框架基础
- hdu 4074 Darts
热门文章
- Linux每日一练20200219
- 安装kubernetes遇见coredns坑
- spring+springMVC+mybatis , 项目启动遇坑
- Linux - 安装 dotnet core 环境
- 吴裕雄--天生自然Django框架开发笔记:Django Admin 管理工具
- [XSS防御]HttpOnly之四两拨千斤
- CountDownLatch和CyclicBarrier和Semaphore最通俗形象解释
- CodeForces - 748D Santa Claus and a Palindrome (贪心+构造)
- Aras Innovator Method集成Visual Studio
- macOS下的播放器