当前位置:网站首页>高攀不起的希尔排序,直接插入排序
高攀不起的希尔排序,直接插入排序
2022-07-01 21:45:00 【老鱼37】
因为期末考试断更了这么久不好意思,从今天起平均每天输出一篇高质量文章供大家享用!!!
First:
直接插入排序:InsertSort
整体思想:

完整代码实现:
//直接插入排序
void InsertSort(int* arr, int len)
{
for (int i = 0; i < len - 1; i++)
{
int end = i;
int tmp = arr[end + 1];
while (end >= 0)
{
if (tmp < arr[end])
{
arr[end + 1] = arr[end];
--end;
}
else
{
break;
}
}
arr[end + 1] = tmp;
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
InsertSort(arr, len);
PrintSort(arr, len);
}下面介绍的是曾经你对他爱答不理,现在你高攀不起的希尔排序
希尔排序是对直接插入排序的优化
整体思想:

1、预排序---将数据变成快要排序好的数据
步骤:先分小组gap,然后再进行排序

当gap到达1的时候就排序好了
是不是很神奇
总结希尔排序特点:
完整代码实现:
//希尔排序
void ShellSort(int* arr, int len)
{
int gap = len;
while (gap > 1)
{
gap = gap / 3 + 1;//保证最后一个数是1
for (int i = 0; i < len - gap; i++)
{
int end = i;
int tmp = arr[end + gap];
while (end >= 0)
{
if (tmp < arr[end])
{
arr[end + gap] = arr[end];
end -= gap;
}
else
{
break;
}
}
arr[end + gap] = tmp;
}
}
}
void PrintSort(int* arr, int len)
{
for (int i = 0; i < len; i++)
{
printf("%d ", arr[i]);
}
}
int main()
{
int arr[] = { 3,1,2,4,9,5,6};
int len = sizeof(arr) / sizeof(arr[0]);
ShellSort(arr, len);
PrintSort(arr, len);
}小伙伴们快点上机操作一下吧!!!
如有错误,请多多指教!
边栏推荐
- Burpsuite simple packet capturing tutorial [easy to understand]
- pytest合集(2)— pytest運行方式
- 91.(cesium篇)cesium火箭发射模拟
- mysql 学习笔记-优化之SQL优化
- 收到一封CTO来信,邀约面试机器学习工程师
- Unity uses SQLite
- PHP reflective XSS, reflective XSS test and repair
- 多种智能指针
- 微信小程序,连续播放多段视频。合成一个视频的样子,自定义视频进度条
- Icml2022 | interventional contrastive learning based on meta semantic regularization
猜你喜欢

基于LSTM模型实现新闻分类

三翼鸟两周年:羽翼渐丰,腾飞指日可待

上半年暂停考试要补考?包含监理工程师、建筑师等十项考试
![[noip2013] building block competition [noip2018] road laying greed / difference](/img/d1/a56231cd4eb3cc1d91d8a55048ccfe.png)
[noip2013] building block competition [noip2018] road laying greed / difference

linux下清理系统缓存并释放内存

I received a letter from CTO inviting me to interview machine learning engineer

locust 系列入门

【直播回顾】战码先锋首期8节直播完美落幕,下期敬请期待!

黑马程序员-软件测试--06阶段2-linux和数据库-01-08第一章-linux操作系统阶段内容说明,linux命令基本格式以及常见形式的说明,操作系统的常见的分类,查看命令帮助信息方法,

Aidl basic use
随机推荐
Manually implement function isinstanceof (child, parent)
【juc学习之路第8天】Condition
Case of camera opening by tour
基础—io密集型计算和cpu密集型计算
Unity 使用Sqlite
游览器打开摄像头案例
函数基本学习之一
Sonic云真机学习总结6 - 1.4.1服务端、agent端部署
[monomer] recommended configuration of streaming information i-bpsv3 server
Design and practice of new generation cloud native database
信标委云原生专题组组长,任重道远!
业务可视化-让你的流程图'Run'起来
String类型转换BigDecimal、Date类型
Sonic cloud real machine learning summary 6 - 1.4.1 server and agent deployment
工控设备安全加密的意义和措施
BlocProvider 为什么感觉和 Provider 很相似?
Unity uses SQLite
linux下清理系统缓存并释放内存
指标陷阱:IT领导者易犯的七个KPI错误
测试撤销1