Java-時間複雜度爲O(nlogn)的排序算法(快速排序, 歸併排序, 堆排序, 希爾排序)

/** 包含多種靜態排序方法
 * Created by Andre on 2016/6/27.
 */
public class Sorter {
    /**
     * 快速排序
     * 遞歸形式
     * 第一個記錄爲樞軸
     * 不穩定
     * @param a 需要排序的數組
     * @param low  數組的起始下標
     * @param high 數組的結束下標
     */
    public static void QuickSort(int a[], int low, int high) {
        if(low >= high) return;

        int pivot = a[low], l = low, h = high;

        for(; l < h; ) {
            for(; l < h && a[h] >= pivot; h--);
            a[l] =  a[h];
            for(; l < h && a[l] <= pivot; l++);
            a[h] = a[l];
        }
        a[l] = pivot;

        QuickSort(a, low, l - 1);
        QuickSort(a, l + 1, high);
    }

    /**
     * 快速排序
     * 遞歸形式
     * 第一記錄爲樞軸
     * @param a 需要排序的數組
     */
    public static void QuickSort(int a[]) {
        QuickSort(a, 0, a.length-1);
    }

    /**
     * 二路歸併排序
     * 遞歸
     * 穩定
     * @param a 需要排序的數組
     * @param low 數組的起始下標
     * @param high 數組的結束下標
     */
    public static void MergeSort(int a[], int low, int high) {
        if(low >= high) return;

        int mid = (low + high) / 2;
        MergeSort(a, low, mid);
        MergeSort(a, mid + 1, high);

        int[] b = new int[high - low + 1];
        int i, j, k = 0;
        for(i = low, j = mid + 1; i <= mid && j <= high; ){
            if(a[i] < a[j])
                b[k++] = a[i++];
            else
                b[k++] = a[j++];
        }
        while(i <= mid)
            b[k++] = a[i++];
        while(j <= high)
            b[k++] = a[j++];

        while(low <= high)
            a[high--] = b[--k];
    }

    /**
     * 二路歸併排序
     * 遞歸
     * @param a 需要排序的數組
     */
    public static void MergeSort(int a[]) {
        MergeSort(a, 0, a.length-1);
    }

    /**
     * 堆排序
     * 不穩定
     * @param a 需要排序的數組
     */
    public static void HeapSort(int[] a){
        for(int i = a.length / 2; i > 0 ; i--)
            percDown(a, i, a.length);
        for(int i = a.length-1; i > 0; i--) {
            int tem = a[0];
            a[0] = a[i];
            a[i] = tem;

            percDown(a, 0, i);
        }
    }

    /**
     * 堆的下濾操作
     * @param a 需要排序的數組
     * @param i 下濾的位置
     * @param n 堆的大小
     */
    public static void percDown(int[] a, int i, int n) {
        int tem = a[i];
        for(int child = 2 * i + 1; child < n; i = child, child = 2 * child + 1) {
            if(child != n-1 && a[child] < a[child+1])
                child++;

            if (tem < a[child])
                a[i] = a[child];
            else
                break;
        }
        a[i] = tem;
    }

    /**
     * 希爾排序
     * 增量序列爲 a.length / 2, a.length / 4, ... 1
     * 不穩定
     * @param a 需要排序的數組
     */
    public static void ShellSort(int[] a) {
        for(int gap = a.length / 2; gap > 0; gap /= 2) {
            for(int i = gap; i < a.length; i++) {
                int tem = a[i], j;
                for(j = i; j >= gap && tem < a[j - gap]; j -= gap)
                    a[j] = a[j - gap];
                a[j] = tem;
            }
        }
    }
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章