Java算法--插入排序、希尔排序、归并排序、堆排序


/**
 * Created by root on 16-3-12.
 */
public class SortAlgorithms <E extends Comparable>{
    //Insertion sort
    public E[] insertionSort(E[] a){
      int length=a.length;
        for(int i=0;i<length;i++){
            for(int j=i;j>0&&a[j].compareTo(a[j-1])<0;j--){
                swap(j,j-1,a);
            }
        }
        return a;
    }
    //shellSort
    public E[] shellSort(E[] a){
        int length=a.length;
        int gap=length/2;
        while(gap>0) {
            for (int i = gap; i < length; i += gap) {
                for (int j = i; j > 0 && a[j].compareTo(a[j - gap]) < 0; j -= gap) {
                    swap(j, j - gap, a);
                }
            }
            gap/=2;
        }
        return a;
    }
        public void mergetSort(E[] a,int left,int right){
        if(left>=right){
            return;
        }
        int center=(left+right)/2;
        mergetSort(a,left,center);
        mergetSort(a,center+1,right);
        merge(a,left,center,right);
    }

    private void merge(E[]a,int left,int center,int right){
        Object[]b=new Object[a.length];
        int leftFloat=left;
        int bIndex=left;
        int rightFloat=center+1;
        while(leftFloat<=center&&rightFloat<=right){
            if(a[leftFloat].compareTo(a[rightFloat])<0){
                b[bIndex++]=a[leftFloat++];
            }
            else {
                b[bIndex++]=a[rightFloat++];
            }
        }
        while (leftFloat<=center){
            b[bIndex++]=a[leftFloat++];
        }
        while (rightFloat<=right){
            b[bIndex++]=a[rightFloat++];
        }
        for(int i=left;i<=right;i++){
            a[i]=(E) b[i];
        }
    }
    
    public void swap(int a,int b,E[] c){
        E tmp=c[a];
        c[a]=c[b];
        c[b]=tmp;
    }
}
/**
 * Created by root on 16-3-12.
 */
public class HeapSort<E extends Comparable> {
    //fields
    private E[] a;
    private int length;
    private int size;
    public Object[] result;

    //constructor
    public HeapSort(E[] a) {
        this.a = a;
        this.length = a.length;
        this.size = length;
        initial();
        result = new Object[length];
        for (int j = 0; j < length; j++) {
            result[j] = this.remove();
        }
    }


    //getChild getParent
    public int getParent(int child) {
        return child / 2;
    }

    public int getChild(int parent) {
        int leftChildIndex = parent * 2 + 1;
        int rightChildIndex = parent * 2 + 2;
        if (rightChildIndex > length - 1) {
            return rightChildIndex;
        }
        int comparison = a[leftChildIndex].compareTo(a[rightChildIndex]);
        if (comparison < 0) {
            return leftChildIndex;
        } else {
            return rightChildIndex;
        }
    }

    //initial
    public void initial() {
        for (int i = length / 2 - 1; i >= 0; i--) {
            filterDown(i);
        }
    }

    //remove
    public E remove() {
        E minItem = a[0];
        E lastItem = a[--size];
        a[0] = lastItem;
        filterDown(0);
        return minItem;
    }

    //filterDown
    public int filterDown(int index) {
        int prev = index;
        int child = getChild(index);
        while (child < size) {
            if (a[prev].compareTo(a[child]) > 0) {
                swap(prev, child);
            } else {
                return 0;
            }
            prev = child;
            child = getChild(prev);
        }
        return prev;
    }

    //swap
    public void swap(int x, int y) {
        E tmp = a[x];
        a[x] = a[y];
        a[y] = tmp;
    }

}
posted @ 2016-03-12 18:55  Salaku  阅读(200)  评论(0编辑  收藏  举报