public class ShellSort {
public static void main(String[] args) {
int[] arr = {13,4,5,6,8,9,5,8,96,17,23};
sort(arr);
String output = "";
for (int i:arr) {
output += i + ",";
}
System.out.println(output);
}
private static void sort(int[] nums) {
int N = nums.length;
int h = 1;
while (h < N) h = h * 3 + 1;
while (h >= 1) {
for (int i = h; i < N; i++) {
for (int j = i; j >= h && nums[j] < nums[j - h]; j -= h ) {
swap(nums, j, j - h);
}
}
h = h / 3;
}
}
private static void swap(int[] nums, int a, int b) {
int temp = nums[a];
nums[a] = nums[b];
nums[b] = temp;
}
}
[算法]ShellSort的Java实现
最新推荐文章于 2025-08-04 00:15:00 发布
本文介绍了一种使用Java实现的希尔排序算法。通过逐步调整增量序列并进行插入排序,该算法能够有效地对整型数组进行排序。文章展示了完整的排序过程及其实现细节。

1万+

被折叠的 条评论
为什么被折叠?



