1. 程式人生 > 其它 >Java 希爾排序

Java 希爾排序

技術標籤:Algorithmjava排序演算法shell

/**
 * 希爾排序
 *
 * @Author ZhangGJ
 * @Date 2020/12/18 06:29
 */
public class Shell {

    public static Comparable[] sort(Comparable[] a) {
        int n = a.length;
        int h = 1;
        while (h < n / 3) {
            h = 3 * h + 1;
        }
        while (h >=
1) { for (int i = h; i < n; i++) { for (int j = i; j >= h && Example.less(a[j], a[j - h]); j -= h) { Example.exchange(a, j, j - h); } } h = h / 3; } return a; } public static
void main(String[] args) { System.out.println(Arrays.toString(sort(new Integer[] {2, 6, 1, 8, 9}))); } }