当前位置:网站首页>七大排序之希尔排序
七大排序之希尔排序
2022-07-27 19:35:00 【威威沁沁】
目录
时间复杂度:
~
空间复杂度:
稳定性:不稳定
1、基本思想
希尔排序是插入排序的一种,又称为缩小增量法。其思想就是先选定一个整数 gap ,把待排序数组中间隔为 gap 的数分为一组,并对每一组内的数进行插入排序。然后,取,重复上述分组排序工作。当 gap = 1时,所有数在同一组内排好序。
2、代码讲解和实现
希尔排序实现可以分为两步:
1:分组
2:组内排序
① 分组
我们首先要给 gap 取值,通常 会把数组的长度先赋值给 gap,然后以gap为步长进行分组排序。接着 gap除以2,再进行上述操作。
最后 我们一定要把数组作为一个整体再进行排序,因为每次分组就肯能落下几个数,所以最后gap 一定要赋值为1。
② 组内排序
组内排序跟插入排序大致相同,在插入排序中外循环是从 1下标开始进行插入,而在希尔排序中 gap 是不断变化的,所以外循环要从 gap 为下标开始,每次++,争取把每个组都遍历到。
内循环 j 从 i - gap 开始,每次减去 gap,这样一组内的元素都趋于有序了。

//组内插入排序
private static void shell(int[] array,int gap){
for (int i = gap; i < array.length; i++) {
int tmp = array[i];
int j = i-gap;
for(; j >= 0; j -= gap){
if(array[j] > tmp){
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);
gap /= 2;
}
shell(array,1);//把数组作为一个整体进行排序3、特性总结
1.希尔排序是对直接插入排序的优化。
2.当gap > 1 时都是预排序,目的是让数组更接近有序。当 gap == 1 时,数组已经接近有序,这样就会很快。这样整体而言,可以达到优化的效果。
3.希尔排序的时间复杂度不好算,因为gap的取值方法很多,导致很难去计算,因此在很多书中给出的希尔排序时间复杂度都不固定。我们可以暂时按照
~
来算。

4.希尔排序不是稳定的排序。
边栏推荐
- Are Transformers Effective for Time Series Forecasting?|填坑
- 7行代码让B站崩溃3小时
- 视频直播源码,uni-app实现广告滚动条
- Live broadcast software app development, uniapp scroll view hidden scroll bar
- 8000字讲透OBSA原理与应用实践
- Is it safe to open an account online now? Then choose which securities to open a securities account
- Time relay
- Chapter 3 business function development (choose to export market activities, Apache POI)
- Finish learning redis cluster solution at one go
- CMOS switch (II)_ Parameter extraction
猜你喜欢

Chapter 3 business function development (choose to export market activities, Apache POI)

What is the employment prospect of software testing?

matlab 绘制三坐标(轴)图
![[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)](/img/c4/71a9933a3a1fdd14f84a41b640f5b5.jpg)
[question 24] logic closed loop (Beijing Institute of Technology / Beijing University of Technology / programming methods and practice / primary school)

Nine days later, we are together to focus on the new development of audio and video and mystery technology

7行代码让B站崩溃3小时

CMOS switch (II)_ Parameter extraction

8000字讲透OBSA原理与应用实践

It seems to be a bug of thread pool, but I think the source code design is unreasonable.

Credit default prediction based on simplified scorecard, smote sampling and random forest
随机推荐
Credit default prediction based on simplified scorecard, smote sampling and random forest
How to learn object Defineproperty | an article takes you to quickly learn
SQL injection less29 (parameter pollution bypasses WAF)
Mask automatic update description file (mask description file)
@Can component be used on the same class as @bean?
[question 23] Sudoku game with rotation | DFS (Beijing Institute of Technology / Beijing Institute of Technology / programming methods and practice / primary school)
Chapter 3 business function development (choose to export market activities, Apache POI)
Leetcode 301. delete invalid parentheses
Time relay
【sql】SQL优化
matlab 绘制三坐标(轴)图
视频直播源码,uni-app实现广告滚动条
[Marine Science] climate indices data set
Why do server programs need to listen first
Massive data TOPK problem
Implementation of arbitrary code execution based on.Net dynamic compilation technology
【图解】三次握手,四次挥手 —— 用心看这一篇就够了
[binary tree] count the number of good nodes in the binary tree
温度继电器
阿里资深软件测试工程师推荐测试人员必学——安全测试入门介绍
