当前位置:网站首页>插入排序与希尔排序
插入排序与希尔排序
2022-07-06 13:56:00 【侯稼澍】
插入排序
将一元素插入到已排序完毕的数组中
可以将无序数组的第一个元素看作 只有一个元素的有序数组
后将数组中的元素一一插入到有序数组中的合适位置
所有元素被插入到有序数组中时,便已将此数组置为有序
动图演示

代码逻辑如下:
以升序为例
将插入的元素通过 tmp保存
假设 此时有序数组中有 i+1 个元素
将有序数组的最后元素(最大的元素)的下标,记为end
通过end遍历数组,寻出元素插入的合适位置
int end = i;
int tmp = a[end + 1];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
}
a[end + 1] = tmp;将 tmp 与 a[end]进行比较,
当 a[end] > tmp 时,将 a[end]后移一位,后 end--
接着与tmp进行比较 ,直至找到合适位置
合适位置的两种情况
1. tmp > end
2.tmp小于数组中所有元素
找到合适位置后,循环便结束,将数据存入合适位置 (end + 1)
此为一个元素插入到有序数组中的步骤
对 n 个元素的数组进行插入排序可以理解为
含有一个元素的有序数组中插入 n-1个元素
转化成代码即为
// 插入排序
void InsertSort(int* a, int n)
{
int i = 0;
for(i=0;i<n-1;i++)
{
int end = i;
int tmp = a[end + 1];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + 1] = a[end];
end--;
}
else
{
break;
}
}
a[end + 1] = tmp;
}
}时间复杂度为 O(N^2)
希尔排序
希尔排序是插入排序的进阶排序,大大优化了插入排序的算法效率
基本思想与代码分析实现
假设数组中共有N个元素
取一小于N的正整数gap
将数组中相距为gap的元素分为一组
注意:各组间元素并不重合,在取元素时,第一组以下标为0的元素为第一个元素
第二种以下标为1的元素为第二个元素
......
将各组元素进行插入排序
int j = 0;
//对每组元素进行排序
for (j = 0; j < gap; j++)
{
int i = 0;
//对一组元素进行排序
for (i = j; i < n - gap; i += gap)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = tmp;
}
}后将 gap减小重复上述步骤
以升序为例子
当 gap > 1时
此过程称之为预排序,为了方便更好更快的进行插入排序
gap越大,大的元素就更快到后方,小的元素就更快到前方
gap越小,数据便越接近有序
当gap == 1时
便为插入排序
综上所述
void ShellSort(int* a, int n)
{
//按照gap 进行插排
int gap = n;
while (gap > 1)
{
gap = gap / 3 + 1;
int j = 0;
for (j = 0; j < gap; j++)
{
int i = j;
for (i = 0; i < n - gap; i += gap)
{
int end = i;
int tmp = a[end + gap];
while (end >= 0)
{
if (a[end] > tmp)
{
a[end + gap] = a[end];
end -= gap;
}
else
{
break;
}
}
a[end + gap] = tmp;
}
}
}
}边栏推荐
- Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
- From campus to Tencent work for a year of those stumbles!
- 20 large visual screens that are highly praised by the boss, with source code templates!
- Unity3D学习笔记6——GPU实例化(1)
- Explain ESM module and commonjs module in simple terms
- Embedded common computing artifact excel, welcome to recommend skills to keep the document constantly updated and provide convenience for others
- 数字化转型挂帅复产复工,线上线下全融合重建商业逻辑
- Wechat red envelope cover applet source code - background independent version - source code with evaluation points function
- 华为在多个行业同时出击,吓人的技术让欧美企业瑟瑟发抖
- What is the RDD operator in spark
猜你喜欢

【sciter】: 基于 sciter 封装通知栏组件

Yyds dry goods inventory C language recursive implementation of Hanoi Tower

50 commonly used numpy function explanations, parameters and usage examples

Sequoia China, just raised $9billion

Uni app app half screen continuous code scanning

UNI-Admin基础框架怎么关闭创建超级管理员入口?

zabbix 代理服务器 与 zabbix-snmp 监控

GPS from getting started to giving up (16), satellite clock error and satellite ephemeris error
![关于char[]数组通过scanf赋值使用上的一些问题。。](/img/cf/d85a3172c5d29ac00377f9c30dbc4f.png)
关于char[]数组通过scanf赋值使用上的一些问题。。

Tiktok will push the independent grass planting app "praiseworthy". Can't bytes forget the little red book?
随机推荐
Univariate cubic equation - relationship between root and coefficient
[asp.net core] set the format of Web API response data -- formatfilter feature
Search element topic (DFS)
C how to set two columns comboboxcolumn in DataGridView to bind a secondary linkage effect of cascading events
Search map website [quadratic] [for search map, search fan, search book]
华为在多个行业同时出击,吓人的技术让欧美企业瑟瑟发抖
小满网络模型&http1-http2 &浏览器缓存
HDU 4912 paths on the tree (lca+)
【sciter Bug篇】多行隐藏
20 large visual screens that are highly praised by the boss, with source code templates!
强化学习-学习笔记5 | AlphaGo
GPS从入门到放弃(十四)、电离层延时
Kohana 数据库
Powerful domestic API management tool
Explain ESM module and commonjs module in simple terms
About the professional ethics of programmers, let's talk about it from the way of craftsmanship and neatness
AI 企业多云存储架构实践 | 深势科技分享
微信红包封面小程序源码-后台独立版-带测评积分功能源码
[Chongqing Guangdong education] Tianjin urban construction university concrete structure design principle a reference
Write a rotation verification code annotation gadget with aardio