基础排序算法 java实现

选择排序

/*
   选择排序
 */
public class Selection {

    public static void main(String[] args) {
        String[] s = {"bed", "bug", "dad", "yes", "zoo", "...", "all", "bad", "yet"};
        sort(s);
        show(s);
    }

    public static void sort(Comparable[] a){
        int length = a.length;
        for (int i = 0; i < length; i++){
            int min = i;
            for (int j = i; j < length; j++){
                if (less(a[j], a[min]) < 0){
                    min = j;
                }
            }
            exch(a, i, min);
        }
    }

    private static int less(Comparable a, Comparable b){
        return a.compareTo(b);
    }

    private static void exch(Comparable[] a, int i, int j){
        Comparable t;
        t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static void show(Comparable[] a){
        for (int i = 0; i < a.length; i++){
            System.out.println(a[i]);
        }
    }
}

插入排序

/*
    插入排序
 */
public class Insertion {
        public static void main(String[] args) {
        System.out.println(args);
        String[] s = {"bed", "bug", "dad", "yes", "zoo", "...", "all", "bad", "yet"};
        sort(s);
        show(s);
    }

    public static void sort(Comparable[] a){
        int length = a.length;
        for (int i = 1; i < length; i++){
            for (int j = i; j > 0 && less(a[j], a[j-1]) < 0; j--)
                exch(a, j, j-1);
        }
    }

    private static int less(Comparable a, Comparable b){
        return a.compareTo(b);
    }

    private static void exch(Comparable[] a, int i, int j){
        Comparable t;
        t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static void show(Comparable[] a){
        for (int i = 0; i < a.length; i++){
            System.out.println(a[i]);
        }
    }
}

希尔排序

/*
    希尔排序
 */

public class Shell {

    public static void main(String[] args){
        String[] s = {"bed", "bug", "dad", "yes", "zoo", "...", "all", "bad", "yet"};
        sort(s);
        show(s);
    }

    public static void sort(Comparable[] a){
        int length = a.length;
        int h = 1;
        while (h < length/3) h = 3*h + 1;
        while (h >= 1){
            for (int i = 0; i < length; i++){
                for (int j = i; j >= h && less(a[j], a[j-h]) < 0; j=j-h){
                    exch(a, j, j-h);
                }
            }
            h = h/3;
        }
    }

    private static int less(Comparable a, Comparable b){
        return a.compareTo(b);
    }

    private static void exch(Comparable[] a, int i, int j){
        Comparable t;
        t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static void show(Comparable[] a){
        for (Comparable c:
             a) {
            System.out.println(c);
        }
    }
}

自顶向下的归并排序

	/*
    自顶向下的归并排序
 */
public class Merge {


    static Comparable[] aux;

    public static void main(String[] args) {
        String[] s = {"bed", "bug", "dad", "yes", "zoo", "...", "all", "bad", "yet"};
        sort(s);
        show(s);
    }

    public static void sort(Comparable[] a){
        int length = a.length;
        sort(a, 0, length-1);
    }

    private static void sort(Comparable[] a, int low, int high){
        if (high <= low) return;
        int mid = low + (high-low)/2;
        sort(a, low, mid);
        sort(a,mid+1, high);
        merge(a, low, mid, high);
    }

    private static void merge(Comparable[] a, int low, int mid, int high){
        aux = new Comparable[high+1];
        for (int i = low; i <= high; i++) aux[i] = a[i];
        int j = mid + 1;
        for (int i = low; i <= high; i++){
            if (low > mid) a[i] = aux[j++];
            else if (high < j) a[i] = aux[low++];
            else if (less(aux[low], aux[j]) < 0) a[i] = aux[low++];
            else a[i] = aux[j++];
        }
    }

    private static int less(Comparable a, Comparable b){
        return a.compareTo(b);
    }

    private static void show(Comparable[] a){
        for (Comparable c : a)
            System.out.println(c);
    }
}

自底向上的归并排序

/*
   自底向上的归并排序
 */
public class MergeBU {

    static Comparable[] aux;

    public static void main(String[] args) {
        String[] s = {"bed", "bug", "dad", "yes", "zoo", "...", "all", "bad", "yet"};
        sort(s);
        show(s);
    }

    public static void sort(Comparable[] a){
        int length = a.length;

        for (int size = 1; size < length; size = size + size){
            for (int low = 0; low < length - size; low += size + size ){
                merge(a, low, low+size-1, Math.min((low+size+size-1), length-1));
            }
        }
    }

    private static void merge(Comparable[] a, int low, int mid, int high){
        aux = new Comparable[high+1];
        for (int i = low; i <= high; i++) aux[i] = a[i];

        int j = mid + 1;
        for (int i = low; i <= high; i++){
            if (j > high) a[i] = aux[low++];
            else if (low > mid) a[i] = aux[j++];
            else if (less(aux[low], aux[j]) < 0) a[i] = aux[low++];
            else a[i] = aux[j++];
        }
    }

    private static int less(Comparable a, Comparable b){
        return a.compareTo(b);
    }

    private static void show(Comparable[] a){
        for (Comparable c : a) System.out.println(c);
    }
}

快速排序

/*
快速排序
 */
public class QuickSort {

    public static void main(String[] args){
        String[] s = {"bed", "bug", "dad", "yes", "zoo", "...", "all", "bad", "yet","yes"};
        sort(s);
        show(s);
    }

    private static void sort(Comparable[] a){
        sort(a, 0, a.length-1);
    }

    public static void sort(Comparable[] a, int low, int high){
        if (low >= high) return;
        int j = partition(a, low, high);
        sort(a, low, j-1);
        sort(a, j+1, high);
    }

    private static int partition(Comparable[] a, int low, int high){
        Comparable pivot = a[low];
        int left = low;
        int right = high;
        int p = low;
        while (true){
            while (less(a[low],pivot) <= 0){
                if (low >= right) break;
                else low++;
            }
            while (less(a[high], pivot) >= 0 ){
                if (high <= left) break;
                else high--;
            }
            if (low >= high) break;
            exch(a, low, high);
        }
        exch(a, p, high);
        return high;
    }

    private static void exch(Comparable[] a, int i, int j){
        Comparable t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static int less(Comparable a, Comparable b){
        return a.compareTo(b);
    }

    private static void show(Comparable[] a){
        for (Comparable c : a)
            System.out.println(c);
    }
}

堆排序

/*
堆排序
 */
public class MaxPQ {

    public static  void main(String[] args){
        String[] s = {"bed", "bug", "dad", "yes", "zoo", "...", "all", "bad", "yet","yes"};
        sort(s);
        show(s);
    }

    private static void sort(Comparable[] b){

        int length = b.length;
        Comparable[] a = new Comparable[length+1];
        for (int i = length;i > 0; i--){
            a[i] = b[i-1];
        }
        for (int i = length/2; i > 0; i--){
            sink(a, i, length);
        }

        while (length > 0){
            exch(a, 1, length);
            length--;
            sink(a, 1, length);
        }

        for (int i = b.length;i > 0; i--){
            b[i-1] = a[i];
        }
    }


    private static void sink(Comparable[] a, int k, int N){
        while (2*k <= N){
            int j = 2 * k;
            if (2*k+1 <= N){
                if (less(a[2*k], a[2*k+1])) j = 2*k + 1;
            }

            if (less(a[k], a[j])) exch(a, k, j);
            k = j;
        }
    }

    private static void exch(Comparable[] a, int i, int j){
        Comparable t;
        t = a[i];
        a[i] = a[j];
        a[j] = t;
    }

    private static Boolean less(Comparable a, Comparable b){
        return a.compareTo(b) < 0;
    }

    private static void show(Comparable[] a){
        for (Comparable c : a) System.out.println(c);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值