Java教程

常见排序算法

本文主要是介绍常见排序算法,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
    /**
     * 快排
     * */
    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;
                }
            }
        }
    }


这篇关于常见排序算法的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!