java基本排序

记录自己整理的几个java排序代码

package coopesa;

public class Index {

    public static void main(String[] args) {

        int[] a = { 1, 4, 5, 6, 8, 2, 3, 9, 6, };
        // selectSort(a);
        // bubleSort(a);
        // insertSort(a);
        // quickSort(a, 0, a.length - 1);
        // quickSort(a, 0, a.length - 1);
        shellSort(a);
        for (int b : a) {
            System.out.println(b);
        }

    }

    // select sort,从后面的列表中选出最大的放到前面
    public static void selectSort(int[] arr) {

        for (int i = 0; i < arr.length - 1; i++) {

            for (int j = i + 1; j < arr.length; j++) {

                if (arr[i] < arr[j]) {
                    int tem = arr[j];
                    arr[j] = arr[i];
                    arr[i] = tem;
                }

            }
        }

    }

    // buble sort,子循环中,两两比较交换,形似冒泡,从而将大的数放到后面
    public static void bubleSort(int[] arr) {

        for (int i = 0; i < arr.length - 1; i++) {

            for (int j = 0; j < arr.length - 1 - i; j++) {

                if (arr[j] > arr[j + 1]) {
                    int tem = arr[j + 1];
                    arr[j + 1] = arr[j];
                    arr[j] = tem;
                }
            }

        }

    }

    // insert sort,假定前面的是有序的,子循环中,用目标元素和前面的比较,遇到大的就向前移动,遇到小的就插入该位置
    public static void insertSort(int[] arr) {

        for (int i = 1; i < arr.length; i++) {

            int j = i;
            int temp = arr[i];
            while (j > 0) {
                if (temp < arr[j - 1]) {

                    arr[j] = arr[j - 1];

                    arr[j - 1] = temp;
                }
                j--;

            }

        }

    }

    // quick sort,类似折半的思想,让某个值左边的都小于它,右边的都大于它,然后两边递归
    public static void quickSort(int[] arr, int low, int high) {
        int start = low;
        int end = high;
        int key = arr[start];

        while (end > start) {

            while (end > start && arr[end] >= key) {
                end--;
            }
            if (arr[end] <= key) {
                int tem = arr[end];
                arr[end] = arr[start];
                arr[start] = tem;
            }
            while (start < end && arr[start] <= key) {
                start++;
            }
            if (arr[start] >= key) {
                int tem = arr[start];
                arr[start] = arr[end];
                arr[end] = tem;
            }

        }

        if (start > low) {
            quickSort(arr, low, start - 1);

        }
        if (end < high) {
            quickSort(arr, end + 1, high);
        }

    }

    // shell sort,类似选择排序,只不过有了增量,先粗后细的思想(先粗略排序,再仔细排序)
    public static void shellSort(int[] arr) {
        int d = arr.length / 2;

        while (d >= 1) {
            for (int i = 0; i < arr.length; i++) {
                for (int j = i; j < arr.length - d; j = j + d) {
                    if (arr[j] > arr[j + d]) {
                        int tem = arr[j];
                        arr[j] = arr[j + d];
                        arr[j + d] = tem;
                    }
                }
            }
            d = d / 2;

        }
    }

}
原创博文,未经许可不得转载,转载请注明出处。
原文地址:https://www.cnblogs.com/Think-007/p/6929324.html