常见排序算法


    /**
     * 快排
     * */
    private static void deepSort(int[] input, int start, int end) {
        boolean turnFlag = false;
        int standIndex = start;
        int left = start, right = end;
        if (left < right) {
            while (left < right){
                if (turnFlag) {
                    if (input[standIndex] < input[left]) {
                        int temp = input[left];
                        input[left] = input[standIndex];
                        input[standIndex] = temp;
                        standIndex = left;
                        turnFlag = false;
                        continue;
                    }
                    ++left;
                } else {
                    if (input[standIndex] > input[right]) {
                        int temp = input[right];
                        input[right] = input[standIndex];
                        input[standIndex] = temp;
                        standIndex = right;
                        turnFlag = true;
                        continue;
                    }
                    --right;
                }
            }
            deepSort(input, start, standIndex - 1);
            deepSort(input, standIndex + 1, end);
        }
    }


    /**
     * 希尔排序
     * */
    public static void sortShell(int[] input){
        int len = input.length;
        for(int group = len / 2; group > 0; group /= 2) {
            for (int i = group; i < len; ++i) {
                for (int j = i; j >= group; j -= group) {
                    if (input[j] < input[j - group]) {
                        int temp = input[j];
                        input[j] = input[j - group];
                        input[j - group] = temp;
                    }
                }
            }
        }
    }


    /**
     * 选择
     * */
    public static void sortX(int[] in) {
        int len = in.length;
        for(int i = 0;i <len; ++i){
            int index = 0;
            for (int j = 0; j < len - i; ++j) {
                if (in[index] < in[j]) {
                    index = j;
                }
            }
            int endIndex = len - (i + 1);
            int temp = in[endIndex];
            in[endIndex] = in[index];
            in[index] = temp;
        }

    }


    /**
     * 冒泡
     * */
    public static void sortM(int[] in){
        int len = in.length;
        for(int i =0;i <len; ++i){
            for (int j = i; j <len; ++j) {
                if (in[i] >= in[j]) {
                    int temp = in[i];
                    in[i] = in[j];
                    in[j] = temp;
                }
            }
        }
    }


posted @ 2022-03-27 02:12  bokerr  阅读(20)  评论(0编辑  收藏  举报