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;
for (j = i; j >= gap && arr[j - gap] > temp; j -= gap) {
arr[j] = arr[j - gap];
}
arr[j] = temp;
}
gap /= 2;
}
}
public static void main(String[] args) {
int[] arr = {3, 5, 1, 9, 2, 8, 7, 6, 4};
sort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
}
评论区