排序算法的思想

2020-02-20  本文已影响0人  走过分叉路

选择排序

public class Selection {
    public static void sort(Comparable[] a){
        for (int i = 0; i < a.length; i++) {
            int min = i; // 最小元素索引
            for (int j = 0; j < a.length; j++) {
                if (less(a[j], a[min])) {
                    min = j;
                }
            }
            exch(a, i, min);
        }
    }
    private static boolean less(Comparable v, Comparable w){
        return v.compareTo(w) < 0;
    }
    private static void exch(Comparable[] a, int i, int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    private static void show(Comparable[] a) {
        // 在单行中打印数组
        for (int i = 0; i < a.length; i++) {
            StdOut.print(a[i] + " ");
        }
        StdOut.println();
    }
    public static boolean isSorted(Comparable[] a) {
        // 测试数组元素是否有序
        for (int i = 0; i < a.length; i++) {
            if (less(a[i], a[i - 1])){
                return false;
            }
        }
        return true;
    }
}

插入排序

public class Insertion {
    public static void sort(Comparable[] a){
        // 将a数组按升序排列
        int N = a.length;
        for (int i = 0; i < N; i++) {
            // 将a[i]插入到a[i-1],a[i-2],a[i-3]...之中
            for (int j = i; j > 0 && less(a[j], a[j-1]); j--){
                exch(a, j, j-1);
            }
        }
    }
    private static boolean less(Comparable v, Comparable w){
        return v.compareTo(w) < 0;
    }
    private static void exch(Comparable[] a, int i, int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    private static void show(Comparable[] a) {
        // 在单行中打印数组
        for (int i = 0; i < a.length; i++) {
            StdOut.print(a[i] + " ");
        }
        StdOut.println();
    }
    public static boolean isSorted(Comparable[] a) {
        // 测试数组元素是否有序
        for (int i = 0; i < a.length; i++) {
            if (less(a[i], a[i - 1])){
                return false;
            }
        }
        return true;
    }
}

希尔排序

public class Shell {
    public static void sort(Comparable[] a){
        // 将a[]按升序排列
        int N = a.length;
        int h = 1;
        while (h < N/3){
            h = 3*h + 1; // 1,4,13,40,121,364,1093,...
        }
        while (h >= 1){
            // 将数组变为h有序
            for (int i = 0; i < N; i++) {
                // 将a[i]插入到a[i-h], a[i-2*h],a[i-3*h]...之中
                for (int j = i; j >= h && less(a[j], a[j-h]); j -= h){
                    exch(a, j, j - h);
                }
                h = h/3;
            }
        }
    }
    private static boolean less(Comparable v, Comparable w){
        return v.compareTo(w) < 0;
    }
    private static void exch(Comparable[] a, int i, int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }
    private static void show(Comparable[] a) {
        // 在单行中打印数组
        for (int i = 0; i < a.length; i++) {
            StdOut.print(a[i] + " ");
        }
        StdOut.println();
    }
    public static boolean isSorted(Comparable[] a) {
        // 测试数组元素是否有序
        for (int i = 0; i < a.length; i++) {
            if (less(a[i], a[i - 1])){
                return false;
            }
        }
        return true;
    }
}

归并排序

public class Merge {
    private static Comparable[] aux; // 归并所需的辅助数组
    private static void sort(Comparable[] a){
        aux = new Comparable[a.length];
        sort(a, 0, a.length - 1);
    }
    private static void sort(Comparable[] a, int lo, int hi){
        // 将数组a[lo..hi]排序
        if (hi <= lo) return;
        int mid = lo + (hi - lo)/2;
        sort(a, lo, mid); // 将左半边排序
        sort(a, mid+1, hi); // 将右半边排序
        merge(a, lo, mid, hi); // 归并结果
    }
    public static void merge(Comparable[] a, int lo, int mid, int hi){
        // 将a[lo..mid]和a[mid+1..hi]归并
        int i = lo, j = mid+1;
        for (int k = 0; k <= hi; k++) { // 将a[lo..hi]复制到aux[lo..hi]
            aux[k] = a[k];
        }

        for (int k = 0; k <= hi; k++) { // 归并回到a[lo..hi]
            if (j > mid)                   a[k] = aux[j++];
            else if (j > hi)               a[k] = aux[i++];
            else if (less(aux[j], aux[i])) a[k] = aux[j++];
            else                           a[k] = aux[i++];
        }
    }
    private static boolean less(Comparable v, Comparable w){
        return v.compareTo(w) < 0;
    }
}
public class MergeBU {
    private static Comparable[] aux; // 归并所需的辅助数组
    public static void sort(Comparable[] a){
        // 进行lgN次两两归并
        int N = a.length;
        aux = new Comparable[N];
        for (int sz = 1; sz < N; sz = sz + sz){ // sz子数组的大小
            for (int lo = 0; lo < N - sz; lo += sz + sz) { // lo:子数组索引
                merge(a, lo, lo+sz-1,Math.min(lo+sz+sz-1, N - 1));
            }
        }
    }
    public static void merge(Comparable[] a, int lo, int mid, int hi){
        // 将a[lo..mid]和a[mid+1..hi]归并
        int i = lo, j = mid+1;
        for (int k = 0; k <= hi; k++) { // 将a[lo..hi]复制到aux[lo..hi]
            aux[k] = a[k];
        }

        for (int k = 0; k <= hi; k++) { // 归并回到a[lo..hi]
            if (j > mid)                   a[k] = aux[j++];
            else if (j > hi)               a[k] = aux[i++];
            else if (less(aux[j], aux[i])) a[k] = aux[j++];
            else                           a[k] = aux[i++];
        }
    }
    private static boolean less(Comparable v, Comparable w){
        return v.compareTo(w) < 0;
    }
}

快速排序

public class Quick {
    public static void sort(Comparable[] a){
        StdRandom.shuffle(a); // 消除对输入的依赖
        sort(a, 0, a.length - 1);
    }
    public static void sort(Comparable[] a, int lo, int hi){
        if (hi <= lo){
            return;
        }
        int j = partition(a, lo, hi);
        sort(a, lo, j - 1); // 将左半部分a[lo..j-1]排序
        sort(a, j + 1, hi); // 将右半部分a[j+1..hi]排序
    }
    private static int partition(Comparable[] a, int lo, int hi){
        // 将数组切分为a[lo..i-1]、a[i]、a[i+1..hi]
        int i = lo, j = hi + 1; // 左右扫描指针
        Comparable v = a[lo]; // 切分元素
        while (true){
            // 扫描左右,检查扫描是否结束并交换元素
            while (less(a[++i], v)){
                if (i == hi){
                    break;
                }
            }
            while (less(v, a[--j])){
                if (j == lo) {
                    break;
                }
            }
            if (i >= j){
                break;
            }
            exch(a, i, j);
        }
        exch(a, lo, j); // 将v = a[j]放入正确的位置
        return j; // a[lo..j-1] <= a[j] <= a[j+1..hi] 达成
    }
    private static boolean less(Comparable v, Comparable w){
        return v.compareTo(w) < 0;
    }
    private static void exch(Comparable[] a, int i, int j){
        Comparable temp = a[i];
        a[i] = a[j];
        a[j] = temp;
    }
上一篇下一篇

猜你喜欢

热点阅读