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] + " ");

}

}

}

相关推荐
没有bug.的程序员3 小时前
服务安全:内部服务如何防止“裸奔”?
java·网络安全·云原生安全·服务安全·零信任架构·微服务安全·内部鉴权
一线大码4 小时前
SpringBoot 3 和 4 的版本新特性和升级要点
java·spring boot·后端
前端小L4 小时前
贪心算法专题(十):维度权衡的艺术——「根据身高重建队列」
javascript·算法·贪心算法
方得一笔4 小时前
自定义常用的字符串函数(strlen,strcpy,strcmp,strcat)
算法
weixin_440730504 小时前
java数组整理笔记
java·开发语言·笔记
weixin_425023004 小时前
Spring Boot 实用核心技巧汇总:日期格式化、线程管控、MCP服务、AOP进阶等
java·spring boot·后端
一线大码4 小时前
Java 8-25 各个版本新特性总结
java·后端
Xの哲學4 小时前
Linux SMP 实现机制深度剖析
linux·服务器·网络·算法·边缘计算
2501_906150564 小时前
私有部署问卷系统操作实战记录-DWSurvey
java·运维·服务器·spring·开源
wuk9984 小时前
使用PCA算法进行故障诊断的MATLAB仿真
算法·matlab