当前位置:网站首页>Sort 1-insert sort and Hill sort
Sort 1-insert sort and Hill sort
2022-07-28 16:31:00 【World_ living】
Preface

One 、 Insertion sort
Direct insertion sort is a simple insertion sort method , Insert the records to be sorted into one by one according to their key values
In an ordered sequence that has been ordered , Until all the records are inserted , We get a new ordered sequence .
In a nutshell : Upper figure 
Of course , The method on the graph inevitably has some special cases
Upper figure : When it comes to the fourth insertion sort 
Of course , When you start writing code , We can write a single trip first , Before hypothesis n-1 It's in order , Let's insert the last number ,
Then write more times , In this way, you can simply write the insertion sort . Of course , I write like this , Is to write the Hill sort introduced below .
void InsertSort(int *arr, int n)// there n yes · The number of elements in the array
{
for (int i = 0; i<n-1; i++)// The picture above shows , When we control the outer cycle , As long as the control is less than n-1
{
int end=i;
int tmp=arr[end+1];
while (end >= 0)
{
if (arr[end] > tmp)// Compare the size
{
arr[end + 1] = arr[end];// Move backward
end--;//end--, Move forward
}
else
{
break;
}
}
arr[end + 1] = tmp;// Prevent the situation mentioned above , Insert... Outside the loop
}
}
summary :
- The closer the elements are to order , The time efficiency of inserting sorting is higher
- The time complexity is O(N)
- The space complexity is O(1)
- stability : Stable
The characteristics of the code inserted above are , The front and back pointers are associated , And from left to right
Two 、 Shell Sort
Shell Sort : Hill sort is also known as reduction increment method , Choose an integer first , Divide all records in the file to be sorted into
Group , All records at a distance of are in the same group , And sort the records in each group . then , take , Repeat the above work of grouping and sorting . When they arrive in =1 when , All records are arranged in a unified group .
To put it simply :
- Pre sort first —> Close to order
- Direct insert sort
Pictured :
When the data is large , How should we control gap Well ?
- We can use data to control , First gap=n
- How to... In the loop gap=(gap/3+1), We use gap/3 Come on, zoom out slowly gap Value
- When gap When I was younger , The closer to order
Why gap=(gap/3+1) instead of gap=(gap/3) Well ?
- for instance , When gap=6 when , If it is gap=(gap/3), So the first cycle ,gap=2, When doing the second cycle ,gap=0 了 , Then you can't play when you go through the following cycle .
- But when gap=(gap/3)+1 When , First cycle ,gap=3, The second cycle ,gap=2, The third cycle gap=1, And then it's over , Now , The insertion sort to be carried out after the pre sort , Sort complete .
- gap=1 It is equivalent to inserting sorting , So control gap To control whether the whole program ends , Of course, when performing insertion sorting , The array is nearly ordered , So at this time, the insertion efficiency is very high .
Careful friends should find out , When in the following code gap=1 Is to insert sort
// Shell Sort
void ShellSort(int *arr,int n )
{
int gap=n;
while (gap>1)
{
gap = (gap / 3 + 1);
for (int i = 0; i < n - gap; i++)
{
int end = i;
int tmp = arr[end + gap];
while (end >= 0)
{
if (arr[end] > tmp)
{
arr[end + gap] = arr[end];
end -= gap;
}
else
{
break;
}
}
arr[end + gap] = tmp;
}
}
}
The outermost part of this code for loop , Take a good look ,
Pictured : This is a gap=3 When 
summary :
- Hill sort is an optimization of direct insert sort .
- When gap > 1 It's all pre sorted , The goal is to make arrays more ordered . When gap == 1 when , The array is nearly ordered , This will soon . So on the whole , Can achieve the effect of optimization . We can compare the performance test after implementation .
- The time complexity of Hill sort is not easy to calculate , You need to deduce , Derive the average time complexity : O(N1.3~N2)
- stability : unstable
summary
Learn from the above , We know that the key to Hill's ranking is not the respective ranking after random grouping , It's going to be separated by something “ The incremental ” The records of , Jump around , Improve the efficiency of sorting .
边栏推荐
- 在abaqus中使用PyQt设计GUI
- Why do most people who learn programming go to Shenzhen and Beijing?
- MySQL view event status statements and modification methods
- PHP计算坐标距离
- 学会使用MySQL的Explain执行计划,SQL性能调优从此不再困难
- Implementation of skip table
- The little red book of accelerating investment, "rush to medical treatment"?
- laravel
- Image semantic segmentation practice: tensorflow deeplobv3+ train your own dataset
- LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)
猜你喜欢
随机推荐
Curl returns blank or null without output. Solve the problem
I can only sell the company after the capital has been "cut off" for two years
我在上海偶遇数字凤凰#坐标徐汇美罗城
在vs code上配置Hypermesh二次开发环境
遭MQ连连干翻后的醒悟!含恨码出这份MQ手册助力秋招之旅
LabVIEW LINX Toolkit控制Arduino设备(拓展篇—1)
ANSA二次开发 - Visual Studio Code上搭建ANSA二次开发环境
“蔚来杯“2022牛客暑期多校训练营3 ACFHJ
laravel
mysql 查看事件状态语句和修改办法
QT QString详解
Ffmpeg get the first frame
Zhaoqi science and technology innovation and entrepreneurship competition talent introduction platform, mass entrepreneurship and entrepreneurship competition high-level talent introduction
Is MySQL query limit 1000,10 as fast as limit 10? If I want to page, what should I do?
Use py to automatically generate weekly reports based on log records
HM二次开发 - Data Names及其使用
What does it remote operation and maintenance mean? Which is the best remote operation and maintenance software?
leetcode 题目
R语言使用fs包的file_delete函数删除指定文件夹下的指定文件、举一反三、dir_delete函数、link_delete函数可以用来删除文件夹和超链接
8051 series MCU firmware upgrade IAP








