Java排序算法优化--Shell排序【温故而知新】

/**
 *
 * @author Fly
 */
public class ShellSort {

    public int[] shellsort(int[] a) {
        int size = a.length;
        for (int step = size / 2; step > 0; step /= 2) {
            for (int i = 0; i < step; i++) {
                for (int j = i; j < size; j += step) {
                    int temp = a[j], k;
                    for (k = j; k > i && a[k - step] > temp; k -= step) {
                        a[k] = a[k - step];
                    }
                    a[k] = temp;
                }
            }
        }
        return a;
    }

    public void printArray(int[] a) {
        for (int i : a) {
            System.out.print(i + ",");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] a = {2, 3, 1, 5, 7, 8, 9, 0, 11, 10, 12, 13, 14, 4, 6};
        ShellSort shellsort = new ShellSort();
        shellsort.printArray(a);
        shellsort.printArray(shellsort.shellsort(a));
    }
}

你可能感兴趣的:(Java排序算法优化--Shell排序【温故而知新】)