六大经典排序算法(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; ii; j--) {
                if(ars[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*d0) {
            for(i=d; i=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=basicValue) {  j--;}
                if(iend) {
            System.arraycopy(ars, i, ars, start+k, middle-i+1);
        }
        System.arraycopy(temp, 0, ars, start, k);
    }
}


你可能感兴趣的:(数据结构算法)