java实现希尔排序

public class ShellSort {

public static void sort(int[] arr) {

int n = arr.length;

int gap = n / 2;

while (gap > 0) {

for (int i = gap; i < n; i++) {

int temp = arr[i];

int j = i;

while (j >= gap && arr[j - gap] > temp) {

arr[j] = arr[j - gap];

j -= gap;

}

arr[j] = temp;

}

gap /= 2;

}

}

public static void main(String[] args) {

int[] arr = {8, 2, 7, 3, 1, 5, 6, 4};

sort(arr);

for (int i = 0; i < arr.length; i++) {

System.out.print(arr[i] + " ");

}

}

}

相关推荐
码出财富33 分钟前
SpringBoot 内置的 20 个高效工具类
java·spring boot·spring cloud·java-ee
多米Domi01142 分钟前
0x3f第33天复习 (16;45-18:00)
数据结构·python·算法·leetcode·链表
我是小疯子661 小时前
Python变量赋值陷阱:浅拷贝VS深拷贝
java·服务器·数据库
森叶1 小时前
Java 比 Python 高性能的原因:重点在高并发方面
java·开发语言·python
二哈喇子!1 小时前
Eclipse中导入外部jar包
java·eclipse·jar
微露清风1 小时前
系统性学习C++-第二十二讲-C++11
java·c++·学习
罗湖老棍子1 小时前
【例4-11】最短网络(agrinet)(信息学奥赛一本通- P1350)
算法·图论·kruskal·prim
方圆工作室1 小时前
【C语言图形学】用*号绘制完美圆的三种算法详解与实现【AI】
c语言·开发语言·算法
Lips6112 小时前
2026.1.16力扣刷题
数据结构·算法·leetcode
进阶小白猿2 小时前
Java技术八股学习Day20
java·开发语言·学习