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 = {5, 9, 3, 1, 8, 6, 4, 2, 7};
    sort(arr);
    for (int i : arr) {
        System.out.print(i + " ");
    }
}

}

写一个希尔排序代码java

原文地址: https://www.cveoy.top/t/topic/bnB3 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录