• 常见排序算法


    
        /**
         * 快排
         * */
        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;
                    }
                }
            }
        }
    
    
    
  • 相关阅读:
    【SICP练习】129 练习3.60
    【SICP练习】128 练习3.59
    【SICP练习】127 练习3.58
    【SICP练习】126 练习3.57
    【SICP练习】125 练习3.56
    【SICP练习】124 练习3.55
    【SICP练习】123 练习3.54
    【SICP练习】122 练习3.53
    【SICP练习】121 练习3.52
    【SICP练习】120 练习3.51
  • 原文地址:https://www.cnblogs.com/bokers/p/16061762.html
Copyright © 2020-2023  润新知