当前位置:网站首页>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 !
边栏推荐
- 2022.07.03 (lc_6111_counts the number of ways to place houses)
- go踩坑——no required module provides package : go.mod file not found in current directory or any parent
- 他做国外LEAD,用了一年时间,把所有房贷都还清了
- P4408 [NOI2003] 逃学的小孩(树的直径)
- What did I pay for it transfer to testing post from confusion to firmness?
- abc 258 G - Triangle(bitset)
- [IELTS reading] Wang Xiwei reading P3 (heading)
- 业务场景功能的继续修改
- js如何实现数组转树
- [selenium automation] common notes
猜你喜欢

分布式BASE理论

海思3559万能平台搭建:YUV422的踩坑记录

Summer challenge brings you to play harmoniyos multi terminal piano performance

URL和URI

Deux nombres se remplacent

Data on the number of functional divisions of national wetland parks in Qinghai Province, data on the distribution of wetlands and marshes across the country, and natural reserves in provinces, cities

初识ROS

图解网络:什么是网关负载均衡协议GLBP?

Paper notes multi UAV collaborative monolithic slam

Detailed explanation of openharmony resource management
随机推荐
Every time I look at the interface documents of my colleagues, I get confused and have a lot of problems...
Complete knapsack problem (template)
Ap8022 switching power supply small household appliances ACDC chip offline switching power supply IC
Upload avatar on uniapp
lambda expressions
It's too convenient. You can complete the code release and approval by nailing it!
Best practice case of enterprise digital transformation: introduction and reference of cloud based digital platform system security measures
JS how to realize array to tree
【北京大学】Tensorflow2.0-1-开篇
If you open an account of Huatai Securities by stock speculation, is it safe to open an account online?
业务实现-日志写到同一个行数据里面
打新债开户注册安全吗?有没有风险的?靠谱吗?
Consolidated expression C case simple variable operation
[IELTS reading] Wang Xiwei reads P4 (matching2 paragraph information matching question [difficult])
如果炒股开华泰证券的户,在网上开户安全吗?
Go step on the pit - no required module provides package: go mod file not found in current directory or any parent
Skills in analyzing the trend chart of London Silver
TS快速入门-函数
OpenHarmony资源管理详解
P4408 [NOI2003] 逃学的小孩(树的直径)