1. 程式人生 > >六大經典排序演算法(Java版):冒泡、選擇、插入、希爾、快速、歸併

六大經典排序演算法(Java版):冒泡、選擇、插入、希爾、快速、歸併

/**
 * @author Darren
 * @Date 2016-10-17
 */
public class Main {
    
    public static void main(String[] args) {
        int[] ars = {2, 5, 9, 13, 28, 79, 2, 87};
        System.out.println("Original Array:");
        myPrintArray(ars);
        int[] ars1 = myBubbleSort(ars);
        System.out.println("BubbleSort:");
        myPrintArray(ars1);

        int[] ars2 = myChooseSort(ars);
        System.out.println("ChooseSort:");
        myPrintArray(ars2);
        
        int[] ars3 = myInsertSort(ars);
        System.out.println("InsertSort:");
        myPrintArray(ars3);
        
        int[] ars4 = myShellSort(ars);
        System.out.println("ShellSort:");
        myPrintArray(ars4);
        
        myFastSort(ars, 0,ars.length-1);
        System.out.println("FastSort:");
        myPrintArray(ars);
        
        int[] ars11 = {2, 5, 9, 87, 28, 79, 2, 13};
        myMergeSort(ars11, 0, ars11.length-1);
        System.out.println("MergeSort:");
        myPrintArray(ars11);
    }
/**
 * @param ars the input array
 */
    public static void myPrintArray(int[] ars) {
        for(int a: ars) {
            System.out.print(a+" ");
        }
        System.out.println();
    }
/**
 * @param ars the input array
 * @return ars the bubble sorted array
 */
    private static int[] myBubbleSort(int[] ars) {
        int i, j, temp;
        for(i=0; i<ars.length; i++) {
            for(j=ars.length-1; j>i; j--) {
                if(ars[j]<ars[j-1]) {
                    temp = ars[j];
                    ars[j] = ars[j-1];
                    ars[j-1] = temp;
                }
            }
        }
        return ars;
    }
/**
 * @param ars the input array
 * @return ars the sorted array
 */
    public static int[] myChooseSort(int[] ars) {
        int i, j, index, temp;
        for(i=0; i<ars.length; i++) {
            index = i;
            for(j=i+1; j<ars.length; j++) {
                if(ars[j]<ars[index]) {
                    index = j;
                }
            }
            temp = ars[index];
            ars[index] = ars[i];
            ars[i] = temp;
        }
        return ars;
    }
/**
 * @param ars the input array
 * @return ars the sorted array
 */
    public static int[] myInsertSort(int[] ars) {
        int i, j, ai;
        for(i=1; i<ars.length; i++) {
            ai = ars[i];
            for(j=i-1; j>=0; j--) {
                if(ars[j]>ai) {
                    ars[j+1] = ars[j];
                }else {
                    break;
                }
            }
            ars[j+1] = ai;
        }
        return ars;
    }
    /**
     * @param ars the input array
     * @return ars the sorted array
     */
    public static int[] myShellSort(int[] ars) {
        int i, j, ai, d=1;
        while(9*d<ars.length) {
            d=3*d+1;
        }
        while(d>0) {
            for(i=d; i<ars.length; i++) {
                ai = ars[i];
                for(j=i-d; j>=0; j-=d) {
                    if(ars[j]>ai) {
                        ars[j+d] = ars[j];
                    }else {
                        break;
                    }
                }
                ars[j+d] = ai;
            }
            d/=3;
        }
        return ars;
    }
/**
 * @param ars the input array
 * @param start the start index of ars
 * @param end the end index of ars
 * @param basicValue the compared value of fast sorting method
 * @return ars the sorted array
 */
    public static void myFastSort(int[] ars, int start, int end) {
        if(start<end) {
            int i=start, j=end, basicValue=ars[start];
            while(i<j) {
                while(i<j && ars[j]>=basicValue) {  j--;}
                if(i<j) {   ars[i] =  ars[j];}
                while(i<j && ars[i]<=basicValue) {  i++;}
                if(i<j) {   ars[j] = ars[i];}
            }
            ars[i] = basicValue;
            myFastSort(ars, start, i-1);
            myFastSort(ars, j+1, end);
        }
    }
/**
 * @param ars the input array
 * @param start the start index of ars
 * @param end the end index of ars
 * @param middle the middle index of merge sorting method
 * @return ars the sorted array
 */
    public static void myMergeSort(int[] ars, int start, int end) {
        if(start<end) {
            int middle = (start+end)/2;
            myMergeSort(ars, start, middle);
            myMergeSort(ars, middle+1, end);
            merge(ars, start, middle, end);
        }
    }
    public static void merge(int[] ars, int start, int middle, int end) {
        int i=start, j=middle+1, k=0;
        int[] temp = new int[end-start+1];
        while(i<=middle && j<=end) {
            temp[k++] = ars[i]<=ars[j] ? ars[i++]:ars[j++];
        }
        if(i<=middle && j>end) {
            System.arraycopy(ars, i, ars, start+k, middle-i+1);
        }
        System.arraycopy(temp, 0, ars, start, k);
    }
}