7種排序算法(java)--第一遍

public class bubbleSort {

    public final static int totalCount = 12;
    public final static int descAsc = 1;// 升序或者降序

    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        Integer[] initNum = new Integer[totalCount];
        int[] initNumo = new int[totalCount];

        for (int i = 0; i < totalCount; i++) {
            initNumo[i] = (int) (Math.random() * 100);
            initNum[i] = Integer.valueOf((int) (Math.random() * 100));
        }
        printNums(initNumo);
        System.out.println("++++++++++++++");

        SortAlgorithm7(initNumo);

        printNums(initNumo);
    }

    // 冒泡排序
    public static void SortAlgorithm1(int initNum[]) {
        int temp = -1;
        if (initNum.length > 1) {
            for (int i = 0; i < initNum.length - 1; i++) {

                for (int j = 0; j < initNum.length - 1 - i; j++) {
                    if (initNum[j] > initNum[j + 1]) {
                        temp = initNum[j];
                        initNum[j] = initNum[j + 1];
                        initNum[j + 1] = temp;
                    }
                }
            }
        }
    }

    // 選擇排序
    public static void SortAlgorithm2(int initNum[]) {
        if (initNum.length < 2) {
            return;
        }
        int temp = initNum[0];
        int tem = -1;
        for (int i = 0; i < initNum.length; i++) {
            for (int j = 0; j < initNum.length - 1 - i; j++) {
                if (initNum[j + 1] > temp) {
                    tem = temp;
                    temp = initNum[j + 1];
                    initNum[j + 1] = tem;

                }
            }
            tem = initNum[initNum.length - 1 - i];
            initNum[initNum.length - 1 - i] = temp;
            temp = tem;
        }
    }

    // 插入排序
    public static void SortAlgorithm3(int initNum[]) {
        int temp = -1;
        for (int i = 1; i < initNum.length; i++) {
            for (int j = 0; j < i; j++) {
                if (initNum[j] <= initNum[i]) {

                } else {
                    temp = initNum[i];
                    for (int f = 0; f < i - j; f++) {
                        initNum[i - f] = initNum[i - f - 1];
                    }
                    initNum[j] = temp;
                }

            }

        }

    }

    // 希爾排序;和步長直接相關
    public static void SortAlgorithm4(int initNum[]) {
        int temp = -1;

        for (int k = initNum.length / 2; k > 0; k = k / 2) {

            for (int i = 1; i < initNum.length; i += k) {
                for (int j = 0; j < i; j += k) {
                    if (initNum[j] <= initNum[i]) {

                    } else {
                        temp = initNum[i];
                        for (int f = 0; f < i - j; f += k) {
                            initNum[i - f] = initNum[i - f - 1];
                        }
                        initNum[j] = temp;
                    }

                }

            }
        }
    }

    // 快速排序
    public static void SortAlgorithm5(int initNum[]) {
        quickSort(initNum, 0, initNum.length - 1);
    }

    public static void quickSort(int initNum[], int left, int right) {
        int temp = -1;
        if (left < right) {
            temp = initNum[left];
            int low = left;
            int high = right;

            while (low < high) {
                while (low < high && initNum[high] >= temp) {
                    high--;
                }
                initNum[low] = initNum[high];
                while (low < high && initNum[low] <= temp) {
                    low++;
                }
                initNum[high] = initNum[low];
            }
            initNum[low] = temp;
            quickSort(initNum, left, low - 1);
            quickSort(initNum, low + 1, right);

        }
    }

    // 堆排序
    public static void SortAlgorithm6(int initNum[]) {
        int temp = -1;
        // 初始化大頂堆
        for (int i = initNum.length / 2 - 1; i >= 0; i--) {
            maxHeadHeap(initNum, i, initNum.length - 1);
        }
        printNumsln(initNum);
        System.out.println();
        // 逐個確定大小
        for (int j = initNum.length - 1; j >= 0; j--) {
            temp = initNum[j];
            initNum[j] = initNum[0];
            initNum[0] = temp;
            maxHeadHeap(initNum, 0, j - 1);
        }

    }

    public static void maxHeadHeap(int initNum[], int index, int indexLast) {

        int leftIndex = index * 2 + 1;
        int rightIndex = index * 2 + 2;
        int maxIndex = index;

        if (leftIndex <= indexLast && initNum[leftIndex] > initNum[maxIndex]) {
            maxIndex = leftIndex;
        }
        if (rightIndex <= indexLast && initNum[rightIndex] > initNum[maxIndex]) {
            maxIndex = rightIndex;
        }
        if (maxIndex != index) {
            int temp = initNum[index];
            initNum[index] = initNum[maxIndex];
            initNum[maxIndex] = temp;
            maxHeadHeap(initNum, maxIndex, indexLast);
        }
    }

    // 調整堆使其成爲大頂堆;index 從0開始
    public static void maxHeadHeap(int initNum[], int index) {

        int leftIndex = index * 2 + 1;
        int rightIndex = index * 2 + 2;
        if (leftIndex > initNum.length) {
            return;
        }
        int maxIndex = index;
        if (initNum[leftIndex] > initNum[index]) {
            maxIndex = leftIndex;
        }
        if (rightIndex < initNum.length
                && initNum[rightIndex] > initNum[maxIndex]) {
            maxIndex = rightIndex;
        }
        if (maxIndex != index) {
            int temp = initNum[index];
            initNum[index] = initNum[maxIndex];
            initNum[maxIndex] = temp;
        }
    }

    // 歸併排序
    public static void SortAlgorithm7(int initNum[]) {
        mergeRecursion(initNum, 0, initNum.length - 1);
    }

    // 歸併遞歸
    public static void mergeRecursion(int initNum[], int start, int end) {

        int middle = (start + end) / 2;
        if (start < end) {
            mergeRecursion(initNum, start, middle);
            mergeRecursion(initNum, middle + 1, end);
            mergeMethed(initNum, start, middle, end);
        }
    }

    public static void mergeMethed(int initNum[], int low, int mid, int high) {
        int low1 = low;
        int low2 = mid+1;
        int tempNum [] = new int[high-low+1];
        int k=0;
        while(low1<mid+1&&low2<=high){
            if(initNum[low1]<=initNum[low2]){
                tempNum[k] = initNum[low1];
                k++;
                low1++;
            }else{
                tempNum[k] = initNum[low2];
                k++;
                low2++;
            }
        }
        while(low1<=mid&&low2>high){
            tempNum[k] = initNum[low1];
            k++;
            low1++;
        }
        while(low2<=high&&low1>mid){
            tempNum[k] = initNum[low2];
            k++;
            low2++;
        }

        for(int j= 0;j<tempNum.length;j++,low++){
            initNum[low]=tempNum[j];
        }

    }

    public static void printNums(int initNum[]) {
        for (int i = 0; i < initNum.length; i++) {
            System.out.println("" + initNum[i]);
        }

    }

    public static void printNums(Integer initNum[]) {
        for (int i = 0; i < initNum.length; i++) {
            System.out.println("" + initNum[i]);
        }

    }

    public static void printNumsln(int initNum[]) {
        for (int i = 0; i < initNum.length; i++) {
            System.out.print("  " + initNum[i]);
        }

    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章