当前位置:网站首页>C语言实现-直接插入排序(带图详解)
C语言实现-直接插入排序(带图详解)
2022-08-01 19:38:00 【Demon--hx】
直接插入排序的实现
一、基本思想
像现实中打扑克牌一样,抓牌就是直接插入排序的很好例子,每抓一张牌插入到合适的位置,最后得到一个有序序列。所以直接插入排序的思想就是把待排序的数字逐个插入到已经排好的有序序列里,最后得到一个新的有序序列。
二、实现思路
①先进行单趟排序:使一个有序区间,插入一个数继续有序,使其依然保持有序。
②再排多趟排序:多趟排序后序列越来越接近有序,最后得到有序序列。
三、图形解释过程
四、代码实现
//直接插入排序
void insertsort(int* a, int n)
{
//多趟数排序,最后为一个有序序列
for (int i = 0; i < n - 1; i++)
{
//单趟排序 [0,end]有序,把值end+1的值插入后,依然有序
int end=i;
int tmp = a[end + 1];
while (end >= 0)
{
if (tmp < a[end])
{
a[end + 1] = a[end];
end--;
}
else
break;
}
a[end + 1] = tmp;
}
}
五、直接插入排序性质总结
边栏推荐
- [Kapok] #Summer Challenge# Hongmeng mini game project - Sudoku (3)
- Write code anytime, anywhere -- deploy your own cloud development environment based on Code-server
- What should I do if the Win11 campus network cannot be connected?Win11 can't connect to campus network solution
- Choosing the right DevOps tool starts with understanding DevOps
- To drive efficient upstream and downstream collaboration, how can cross-border B2B e-commerce platforms release the core value of the LED industry supply chain?
- 哈哈!一个 print 函数,还挺会玩啊!
- 10 个 PHP 代码安全漏洞扫描程序
- Heavy cover special | build the first line of defense, cloud firewall offensive and defensive drills best practices
- 第57章 业务逻辑之业务实体与数据库表的映射规则定义
- 【服务器数据恢复】服务器Raid5阵列mdisk组中多块磁盘离线的数据恢复案例
猜你喜欢
随机推荐
重保特辑|拦截99%恶意流量,揭秘WAF攻防演练最佳实践
数据库系统原理与应用教程(070)—— MySQL 练习题:操作题 101-109(十四):查询条件练习
Pytorch模型训练实用教程学习笔记:三、损失函数汇总
突破边界,华为存储的破壁之旅
57: Chapter 5: Develop admin management services: 10: Develop [get files from MongoDB's GridFS, interface]; (from GridFS, get the SOP of files) (Do not use MongoDB's service, you can exclude its autom
为你的“架构”安排定期体检吧!
PHP 安全最佳实践
Risc-v Process Attack
Win11如何删除升级包?Win11删除升级包的方法
开源视界 | StreamNative 盛宇帆:和浪漫的人一起做最浪漫的事
{ValueError}Number of classes, 1, does not match size of target_names, 2. Tr
【1374. 生成每种字符都是奇数个的字符串】
Every calculation, & say what mean
LabVIEW 使用VISA Close真的关闭COM口了吗
57:第五章:开发admin管理服务:10:开发【从MongoDB的GridFS中,获取文件,接口】;(从GridFS中,获取文件的SOP)(不使用MongoDB的服务,可以排除其自动加载类)
即时通讯开发移动端弱网络优化方法总结
ExcelPatternTool: Excel form-database mutual import tool
Pytorch模型训练实用教程学习笔记:一、数据加载和transforms方法总结
regular expression
【Redis】缓存雪崩、缓存穿透、缓存预热、缓存更新、缓存击穿、缓存降级