当前位置:网站首页>Insert sort and Hill sort
Insert sort and Hill sort
2022-07-06 22:05:00 【Hou Jiashu】
Insertion sort
Insert an element into the sorted array
You can think of the first element of an unordered array as An ordered array with only one element
Then insert the elements in the array one by one into the appropriate position in the ordered array
When all elements are inserted into an ordered array , This array has been set to order
Dynamic diagram demonstration

The code logic is as follows :
Take ascending order
Pass the inserted element through tmp preservation
hypothesis At this time, there are i+1 Elements
The last element of the ordered array ( Largest element ) The subscript , Write it down as end
adopt end Traversal array , Find out the appropriate position for element insertion
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;take tmp And a[end] Compare ,
When a[end] > tmp when , take a[end] Move back one position , after end--
Then with tmp Compare , Until you find the right place
Two cases of suitable location
1. tmp > end
2.tmp Less than all elements in the array
When you find the right place , The cycle ends , Store the data in the right place (end + 1)
This is the step of inserting an element into an ordered array
Yes n Inserting and sorting an array of elements can be understood as
Insert... Into an ordered array containing one element n-1 Elements
When converted into code, it is
// Insertion sort
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;
}
}The time complexity is O(N^2)
Shell Sort
Hill sort is an advanced sort of insert sort , Greatly optimize the efficiency of the insertion sort algorithm
Basic idea and code analysis implementation
Suppose the array has N Elements
Take one less than N The positive integer gap
Set the distance in the array to gap The elements of are divided into a group
Be careful : The elements between the groups do not coincide , When taking elements , The first group is marked below 0 The element of is the first element
The second type is marked as 1 The element of is the second element
......
Insert and sort each group of elements
int j = 0;
// Sort each group of elements
for (j = 0; j < gap; j++)
{
int i = 0;
// Sort a set of elements
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;
}
}After the gap Reduce and repeat the above steps
Take ascending order as an example
When gap > 1 when
This process is called pre sorting , In order to facilitate better and faster insertion sorting
gap The bigger it is , Large elements will reach the rear faster , Small elements move faster to the front
gap The smaller it is , The closer the data is to order
When gap == 1 when
Sort for insertion
in summary
void ShellSort(int* a, int n)
{
// according to gap Do the nesting
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;
}
}
}
}边栏推荐
- Oracle性能分析3:TKPROF简介
- Explain ESM module and commonjs module in simple terms
- HDU 4912 paths on the tree (lca+)
- 1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
- [Yu Yue education] reference materials for surgical skills teaching in Tongji University
- GPS從入門到放弃(十三)、接收機自主完好性監測(RAIM)
- 【sciter Bug篇】多行隐藏
- UNI-Admin基础框架怎么关闭创建超级管理员入口?
- [sciter bug] multi line hiding
- GPS du début à l'abandon (XIII), surveillance autonome de l'intégrité du récepteur (raim)
猜你喜欢

GPS from getting started to giving up (XIII), receiver autonomous integrity monitoring (RAIM)
![Leetcode topic [array] -118 Yang Hui triangle](/img/77/d8a7085968cc443260b4c0910bd04b.jpg)
Leetcode topic [array] -118 Yang Hui triangle

PostgreSQL 修改数据库用户的密码

The golden age of the U.S. technology industry has ended, and there have been constant lamentations about chip sales and 30000 layoffs
![[MySQL] online DDL details](/img/7e/97098d7ed5802c446bbadaf7035981.png)
[MySQL] online DDL details

C#实现水晶报表绑定数据并实现打印4-条形码

AI enterprise multi cloud storage architecture practice | Shenzhen potential technology sharing

1292_FreeROS中vTaskResume()以及xTaskResumeFromISR()的实现分析
![关于char[]数组通过scanf赋值使用上的一些问题。。](/img/cf/d85a3172c5d29ac00377f9c30dbc4f.png)
关于char[]数组通过scanf赋值使用上的一些问题。。

GPS从入门到放弃(十六)、卫星时钟误差和卫星星历误差
随机推荐
Leetcode topic [array] -118 Yang Hui triangle
Qt | UDP广播通信、简单使用案例
关于程序员的职业操守,从《匠艺整洁之道》谈起
Unity3D学习笔记6——GPU实例化(1)
嵌入式常用计算神器EXCEL,欢迎各位推荐技巧,以保持文档持续更新,为其他人提供便利
Method return value considerations
Leetcode learning records (starting from the novice village, you can't kill out of the novice Village) ---1
美国科技行业结束黄金时代,芯片求售、裁员3万等哀声不断
Search map website [quadratic] [for search map, search fan, search book]
High precision face recognition based on insightface, which can directly benchmark hongruan
Problems in the process of opencv300 cmake generating project
20 large visual screens that are highly praised by the boss, with source code templates!
Force buckle 575 Divide candy
NPM run dev start project error document is not defined
GPS from getting started to giving up (XVIII), multipath effect
Digital transformation takes the lead to resume production and work, and online and offline full integration rebuilds business logic
Depth first traversal (DFS) and breadth first traversal (BFS)
The underlying implementation of string
HDU 4912 paths on the tree (lca+)
Mysql相关术语