• 算法导论读书笔记(8)


    算法导论读书笔记(8)

    计数排序

    计数排序 假设 n 个输入元素中的每一个都是介于0到 k 之间的整数,此处 k 为某个整数。当 k = O ( n )时,计数排序的运行时间为 Θ ( n )。

    计数排序的基本思想就是对每一个输入元素 x ,确定出小于 x 的元素个数。有了这一信息,就可以把 x 直接放到它在最终输出数组上的位置上。

    COUNTING-SORT(A, B, k)
    1  let C[0 .. k] be a new array
    2  for i = 0 to k
    3      C[i] = 0
    4  for j = 1 to A.length
    5      C[A[j]] = C[A[j]] + 1
    6  // C[i] now contains the number of elements equal to i.
    7  for i = 1 to k
    8      C[i] = C[i] + C[i - 1]
    9  // C[i] now contains the number of elements less than or equal to i.
    10 for j = A.length downto 1
    11     B[C[A[j]]] = A[j]
    12     C[A[j]] = C[A[j]] - 1
    

    计数排序的一个重要性质是它是稳定的:具有相同值得元素在输出数组中的相对次序与它们在输入数组中的次序相同。而且,计数排序经常作为基数排序算法的一个子程序。

    计数排序的简单Java实现

    /**
     * 计数排序
     */
    public static int[] countingSort(int[] array, int k) {
        int[] result = new int[array.length];
        int[] temp = new int[k];
        for (int i = 0; i < k; i++)
            temp[i] = 0;
        for (int j = 0; j < array.length; j++)
            temp[array[j]]++;
        for (int i = 1; i < k; i++)
            temp[i] = temp[i] + temp[i - 1];
        for (int j = array.length - 1; j >= 0; j--) {
            result[temp[array[j]] - 1] = array[j];
            temp[array[j]]--;
        }
        return result;
    }
    

    基数排序

    基数排序 要求待排序的元素拥有相同的位数。从元素的低位到高位依次排序。

    RADIX-SORT(A, d)
    1 for i = 1 to d
    2     use a stable sort to sort array A on digit i
    

    基数排序的简单Java实现

    public static void radixSort(int[] array) {
        int len = String.valueOf(array[0]).toString().length();
        for (int i = 0; i < len; i++)
            radixQuickSort(array, i);
    }
    
    /**
     * 计数排序的变形
     */
    public static void radixQuickSort(int[] array, int power) {
        int[] result = new int[array.length];
        int[] temp = new int[10];
        for (int i = 0; i < 10; i++)
            temp[i] = 0;
        for (int j = 0; j < array.length; j++)
            temp[getDigit(array[j], power)]++;
        for (int i = 1; i < 10; i++)
            temp[i] = temp[i] + temp[i - 1];
        for (int j = array.length - 1; j >= 0; j--) {
            result[temp[getDigit(array[j], power)] - 1] = array[j];
            temp[getDigit(array[j], power)]--;
        }
        for (int i = 0; i < result.length; i++)
            array[i] = result[i];
    }
    
    /**
     * 获得数字第n位的值
     */
    public static int getDigit(int value, int power) {
        return (int) (value / Math.pow(10, power)) % 10;
    }
    

    桶排序

    桶排序 (bucket sort)的输入符合均匀分布时,它可以以线性时间运行。桶排序假设输入由一个随机过程产生,该过程将元素均匀地分布在区间[ 0, 1 )上。

    桶排序的思想就是把区间[ 0, 1 )划分成 n 个相同大小的子区间,或称 。然后,将 n 个输入数分布到各个桶中去。因为输入数均匀分布在[ 0, 1 )上,所以,一般不会有很多数落在一个桶中的情况。为得到结果,先对各个桶中的元素进行排序,然后按次序把桶中的元素列出来即可。

    BUCKET-SORT(A)
    1  let B[0 .. n - 1] be a new array
    2  n = A.length
    3  for i = 0 to n - 1
    4      make B[i] an empty list
    5  for i = 1 to n
    6      insert A[i] into list B[FLOOR(nA[i])]
    7  for i = 0 to n - 1
    8      sort list B[i] with insertion sort
    9  concatenate the lists B[0], B[1], ..., B[n - 1] together in order
    
  • 相关阅读:
    Javascript内容整理——BOM
    前端实现excel报表,vue+luckysheet
    代码转图片 的一个好用网站
    推荐一个非常好用前端在线编辑器!!! 一点都不卡
    前端常用的一些网站
    vue中使用moment,如何按需打包?
    better-scroll
    时间线
    碎纸屑动画
    动画
  • 原文地址:https://www.cnblogs.com/sungoshawk/p/3646265.html
Copyright © 2020-2023  润新知