当前位置:网站首页>C语言插入排序(直接插入排序)
C语言插入排序(直接插入排序)
2022-07-23 06:13:00 【恶龙咆哮@~】
直接插入排序:把待排序的记录按其关键码值的大小逐个插入到一个已经排好序的有序序列中,直到所有的记录插入完为止,得到一个新的有序序列 。
时间复杂度:O(N^2);
空间复杂度:O(1);
稳定性:稳定排序
应用场景:序列接近有序或者元素个数较少的情况
动图演示
void InsertSort(int arr[], int size)
{
//i不能<=size-1;否则不稳定
for (int i = 1; i < size-1; i++)
{
int end = i-1;
int key = arr[i];
//end>=0,防止数组越界
while (end >=0 && arr[end] > key)
{
arr[end + 1] = arr[end];
end--;
}
arr[end+1] = key;
}
}
边栏推荐
- Course design - push box C (win form)
- PHP framework MVC class code audit
- CORTEX-A系列处理器
- Beifu PLC and C transmit bool array variables through ads communication
- 分类模型的评估
- Jenkins continuous integration error stderr: fatal: unsafe repository ('/home/water/water' is owned by someone else)
- Quelle est la raison pour laquelle la plate - forme easygbs ne peut pas lire l'enregistrement vidéo et a un phénomène de streaming répété rtmp?
- Bit synchronization process of CAN controller
- Beifu PLC and C transmit string array type variables through ads communication
- 高压MOS管KNX42150 1500V/3A 应用于变频器电源-逆变器等
猜你喜欢

Beifu PLC and C transmit string type through ads communication

迷茫、工作没动力? 职业发展没希望? 看这篇文章就够了
![[actf2020 freshman competition]backupfile 1](/img/4c/cefb3660a176fee7fde6b0e38e6f4b.png)
[actf2020 freshman competition]backupfile 1

【JZOF】12矩阵中的路径

【JZOF】07 重建二叉树

Are there any academic requirements for career transfer software testing? Is there really no way out below junior college?

谈谈学习和工作——钱学森

Why build a local Yum warehouse?

分类模型的评估

当输入网址后,到网页显示,期间发生了什么
随机推荐
[daily training] 814. Binary tree pruning
Beifu PLC -- C ads communication reads variables in the form of notification
倍福PLC和C#通过ADS通信传输String数组类型变量
php框架MVC类代码审计
[jzof] 07 rebuild binary tree
第八天笔记
Beifu PLC and C transmit string type through ads communication
从List<Map>中截取指定的范围数据集合
太空射击 Part 2-3: 子弹与敌人碰撞处理
[jzof] 10 Fibonacci series
射击 第 1-01 课:入门
分类模型的评估
redis分布式锁实践
GOM引擎版本为什么玩家会自动掉线或闪退?
PKU doctor's little sister: sharing dry goods at the bottom of the box | five tips to improve learning efficiency
【JZOF】08 二叉树的下一个结点
北大博士小姐姐:分享压箱底干货 | 五招提高学习效率
How does redis implement persistence? Explain in detail the three triggering mechanisms of RDB and their advantages and disadvantages, and take you to quickly master RDB
转行软件测试有学历要求吗?低于大专是真的没出路吗?
太空射击 Part 1: 玩家精灵和控制