当前位置:网站首页>Hill sort of sorting
Hill sort of sorting
2022-07-05 00:28:00 【Building Zzzz】
Catalog
Two 、 Specific code implementation
3、 ... and 、 Spatial complexity , Time complexity and stability
Preface
Previously, we learned about direct insertion sorting , Direct insertion sorting is used when the data is in reverse order , Time complexity will reach O(N^2), This is a high time complexity for a sort , So today we will optimize the direct insertion sort , The more ordered the data, the faster the speed of direct insertion sorting is used to optimize it .
One 、 What is Hill sort
Hill's ranking method is also called reducing increment method . The basic idea of Hill's ranking is : Divide all the records in the file to be sorted into groups ( The specific grouping is a complex problem , This function changes with the data , It's an unsolved problem , So far, there is no good incremental sequence to solve , It can be said that this incremental sequence can be taken in various ways , However, it should be noted that the value in the incremental sequence should not be divided 1 The common factor outside is to try to be prime , And the last increment value must be taken 1 To end the overall sorting , All the previous sorts are pre sort , The last increment sequence is 1 Time is the real sort ), All records with the distance of this group number are divided into the same group , And sort the records in each group . Then repeat the above grouping and sorting . When the number of groups reaches 1 when , The sorting of the overall number of groups can be completed by arranging all data records in a unified group . The specific sorting method still uses direct insertion sorting, but as the whole sequence becomes more and more orderly, the speed will be faster , Therefore, Hill sort is the optimization of direct insertion sort .

Two 、 Specific code implementation
/**
* In fact, it is a direct insertion sort
* @param array Sequence to be sorted
* @param gap Group number
*/
public static void shell(int[] array,int gap) {
for (int i = gap + 1; i < array.length; i++) {
int j = i - gap;
int tmp = array[i];
for (; j >= 0; j -= gap) {
if(tmp < array[j]){
array[j + gap] = array[j];
}else{
break;
}
}
array[j + gap] = tmp;
}
}
public static void shellSort(int[] array) {
int gap = array.length;
while (gap > 1) {
shell(array,gap);// Sort by changing the number of groups each time
gap /= 2;
}
shell(array,1);// Make sure the end is 1 Group
}3、 ... and 、 Spatial complexity , Time complexity and stability
Time complexity :( Increment related )O(n ^ 1.3 - n ^ 1.5) Spatial complexity : O(1)( No more variables are used ) stability : unstable ( If there is a jump change in the sorting process, it is unstable )
summary
Hill sort is an optimization of direct sort , In fact, we can divide the specific groups randomly, but in the end, we must press 1 To distribute , Then it is to sort the whole , This also optimizes the insertion sort , But Hill sort is rarely used , But we must know how to do this !
边栏推荐
- Summer challenge brings you to play harmoniyos multi terminal piano performance
- js如何实现数组转树
- Kibana index, mapping, document operation
- He worked as a foreign lead and paid off all the housing loans in a year
- The waterfall flow layout demo2 (method 2) used by the uniapp wechat applet (copy and paste can be used without other processing)
- Complete knapsack problem (template)
- Illustrated network: what is gateway load balancing protocol GLBP?
- IELTS examination process, what to pay attention to and how to review?
- uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)
- Daily practice (18): stack containing min function
猜你喜欢

What is the difference between port mapping and port forwarding

人脸识别5- insight-face-paddle-代码实战笔记

Verilog tutorial (11) initial block in Verilog

兩個數相互替換

企业公司项目开发好一部分基础功能,重要的事保存到线上第一a
![[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection](/img/25/e2366cabf00e55664d16455a6049e0.png)
[论文阅读] TUN-Det: A Novel Network for Thyroid Ultrasound Nodule Detection

uniapp微信小程序拿来即用的瀑布流布局demo2(方法二)(复制粘贴即可使用,无需做其他处理)

lambda expressions

How many triangles are there in the golden K-line diagram?

Detailed explanation of openharmony resource management
随机推荐
Deux nombres se remplacent
ORB(Oriented FAST and Rotated BRIEF)
skimage: imread & imsave & imshow
实战模拟│JWT 登录认证
How to do the project of computer remote company in foreign Internet?
Face recognition 5- insight face padding code practice notes
Complete knapsack problem (template)
业务场景功能的继续修改
Tester's algorithm interview question - find mode
TS快速入门-函数
Life is changeable, and the large intestine covers the small intestine. This time, I can really go home to see my daughter-in-law...
【路径规划】RRT增加动力模型进行轨迹规划
(脚本)一键部署redis任意版本 —— 筑梦之路
【雅思阅读】王希伟阅读P4(matching1)
Some basic functions of enterprise projects are developed, and important things are saved to online first a
【C】 (written examination questions) pointer and array, pointer
Is it safe to open an account in the College of Finance and economics? How to open an account?
Nine Qi single chip microcomputer ny8b062d single key control four LED States
Advanced template
Summer challenge brings you to play harmoniyos multi terminal piano performance