排序算法总结:选择,冒泡,插入,归并,快排,堆排

使用Java实现常见的排序算法

public class Test2_ArraySort {

    // 获取指定长度的数组
    private int[] getArr(int length) {
        int[] re = new int[length];
        for (int i = 0; i < re.length; i++) {
            re[i] = (int) (Math.random() * 100 + 10);
        }
        return re;
    }

    // 打印指定的函数
    private void printArray(String message, int[] arr) {
        System.out.println(message + " : " + Arrays.toString(arr));
    }

    // 重新生成数组
    private void shuffle(int[] re) {
        for (int i = 0; i < re.length; i++) {
            re[i] = (int) (Math.random() * 100 + 10);
        }
    }

    // 交换数组中元素的位置
    private void swap(int[] arr, int i, int j) {
        int temp = arr[i];
        arr[i] = arr[j];
        arr[j] = temp;
    }

    // 选择法排序
    private void XZSort(int[] arr, BiPredicate<Integer, Integer> judge) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (judge.test(arr[i], arr[j])) {
                    swap(arr, i, j);
                }
            }
        }
    }

    // 冒泡法排序
    private void MPSort(int[] arr, BiPredicate<Integer, Integer> judge) {
        for (int i = 0; i < arr.length - 1; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if (judge.test(arr[j], arr[j + 1])) {
                    swap(arr, j, j + 1);
                }
            }
        }
    }

    // 插入法排序
    private void CRSort(int[] arr, BiPredicate<Integer, Integer> judge) {
        for (int i = 1; i < arr.length; i++) {
            int j = i;
            while (j - 1 >= 0 && judge.test(arr[j], arr[j - 1])) {
                swap(arr, j, j - 1);
                j--;
            }
        }
    }

    // 归并排序
    private int[] GBSort(int[] arr, BiPredicate<Integer, Integer> judge) {
        if (arr.length < 2) {
            return arr;
        } else {
            int mid = arr.length / 2;
            int[] left = Arrays.copyOfRange(arr, 0, mid);
            int[] right = Arrays.copyOfRange(arr, mid, arr.length);
            left = GBSort(left, judge);
            right = GBSort(right, judge);
            int[] re = new int[left.length + right.length];
            int index = 0;
            int i = 0, j = 0;
            while (i < left.length && j < right.length) {
                if (judge.test(left[i], right[j])) {
                    re[index++] = left[i++];
                } else {
                    re[index++] = right[j++];
                }
            }
            while (i < left.length) {
                re[index++] = left[i++];
            }
            while (j < right.length) {
                re[index++] = right[j++];
            }
            return re;
        }
    }

    // 快排
    private void KSort(int[] arr, int start, int end, BiPredicate<Integer, Integer> judge) {
        if (end - start >= 1) {
            int anchor = (int) (Math.random() * (end - start + 1) + start);
            swap(arr, anchor, end);
            int i = start;
            int j = start;
            while (j < end) {
                if (judge.test(arr[j], arr[end])) {
                    swap(arr, j, i);
                    i++;
                }
                j++;
            }
            swap(arr, i, end);
            KSort(arr, start, i - 1, judge);
            KSort(arr, i + 1, end, judge);
        }
    }

    // 对堆进行重新排序
    private void headDown(int[] arr, int start, int end, BiPredicate<Integer, Integer> judge) {
        int target = start;
        int left = start * 2 + 1;
        int right = start * 2 + 2;
        if (left < end && judge.test(arr[target], arr[left])) {
            target = left;
        }
        if (right < end && judge.test(arr[target], arr[right])) {
            target = right;
        }
        if (target != start) {
            swap(arr, target, start);
            headDown(arr, target, end, judge);
        }
    }

    // 生成指定类型的堆
    private void headIfy(int[] arr, BiPredicate<Integer, Integer> judge) {
        for (int i = (arr.length - 1) / 2; i >= 0; i--) {
            headDown(arr, i, arr.length, judge);
        }
    }

    // 堆排序
    private void headSort(int[] arr, BiPredicate<Integer, Integer> judge) {
        // 首先生成一个堆
        headIfy(arr, judge);
        for (int i = 0; i < arr.length; i++) {
            // 生成堆后顶部就是最值
            swap(arr, 0, arr.length - 1 - i);
            // 重新进行堆排序
            headDown(arr, 0, arr.length - 1 - i, judge);
        }
    }

    public static void main(String[] args) {
        Test2_ArraySort ta = new Test2_ArraySort();
        int[] src = ta.getArr(100);
        ta.printArray("原始数组", src);
        ta.XZSort(src, (n1, n2) -> n1 > n2);
        ta.printArray("选择排序,逆序", src);
        ta.shuffle(src);
        ta.printArray("重新生成", src);
        ta.MPSort(src, (n1, n2) -> n1 > n2);
        ta.printArray("冒泡排序, 逆序", src);
        ta.shuffle(src);
        ta.printArray("重新生成", src);
        ta.CRSort(src, (n1, n2) -> n1 < n2);
        ta.printArray("插入排序, 逆序", src);
        ta.shuffle(src);
        ta.printArray("重新生成", src);
        src = ta.GBSort(src, (n1, n2) -> n1 < n2);
        ta.printArray("归并排序, 逆序", src);
        ta.shuffle(src);
        ta.printArray("重新生成", src);
        ta.KSort(src, 0, src.length - 1, (n1, n2) -> n1 < n2);
        ta.printArray("快排, 逆序", src);
        ta.shuffle(src);
        ta.printArray("重新生成", src);
        ta.headSort(src, (n1, n2) -> n1 < n2);
        ta.printArray("堆排, 逆序", src);
    }
}

你可能感兴趣的:(Java,java,数据结构,排序算法,堆排序,快速排序)