JAVA拾遺 - 希爾排序

package Elementary_Sorts;

/**
 * Created by coco1 on 2016/9/18.
 * 希爾排序(Shell Sort)是插入排序的一種。
 * 也稱縮小增量排序,是直接插入排序算法的一種更高效的改進版本。
 * 希爾排序是非穩定排序算法。
 * 該方法因DL.Shell於1959年提出而得名。
 *
 * Insertion Sort is inefficient because elements really move only one position at time even we've know
 * that they have a long way to go
 *
 * In Shell Sort we'll move entries serval positions at a time
 *
 * Instead Insertion Sort, which going one back every time we come with a new item, we go h back.
 *
 * because H-sort J-sort K-sort when we try to 1-sort the array is mostly in order
 *
 * After J-sort , a H-sort array is still sorted by H-sort <---that's why shell sort gain it's efficient!
 *
 * How to select H、J、K:
 *
 * power of 2 : bad
 *
 * power of 2 - 1 : maybe
 *
 * 3x + 1 : easy to compute
 *
 * 1 5 19 41 109 209 505 929 2161 3905 ... :Nobody know this is the best!
 */
public class ShellSort {
    public static void shellSort(Comparable[] a) {
        int N = a.length;
        int h = 1;
        while (h<N/3) {h = 3 * h + 1;}//cacu the h
        while (h >= 1) {
            for (int i = h ; i < N ; i ++) {
                for (int j = i ; j >= h && less(a[j] , a[j - h]) ; j -=h) {
                    exch(a, j , j -h);
                }
            }
            h = h / 3;
        }
    }
    public static boolean less(Comparable a, Comparable b) {
        if (a.compareTo(b) > 0) return true;
        else {
            return false;
        }
    }
    public static void exch(Comparable[] a, int i, int min) {
        Comparable swap = a[i];
        a[i] = a[min];
        a[min] = swap;
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章