浅谈排序算法之计数排序(7)

接下来的三篇博客里,笔者将会简单聊聊三种线性时间复杂度的排序算法,即计数排序、基数排序以及桶排序,均出自《算法导论》。
计数排序(counting sort)假设数组A中的所有元素都是0~k区间内的一个整数,其中k为某个整数值。计数排序的思想是对每一个数组元素x,计算小于x的元素的个数。计数排序的实现过程分为三步:

  1. 计算数组A中每个元素出现的次数。
  2. 统计数组A中存在多少个元素小于等于A中的指定元素。
  3. 从后往前依次遍历数组A,将每个元素放置在输出数组中正确的位置上。

示例代码如下:

package org.vimist.pro.Algorithm.Sort;

import org.jetbrains.annotations.NotNull;

import java.util.Arrays;
import java.util.Random;

/**
 * An illustration of {@code CountingSort}.
 *
 * @author Mr.K
 */
public class CountingSort {

    /**
     * length of array to be sorted
     */
    private static int N = 20;

    /**
     * upper bound for element in array
     */
    private static int UpperBound = 2 * N;

    public static void main(String[] args) {
        int[] arr = new int[N];
        Random random = new Random();
        for (int i = 0; i < arr.length; i++) {
            arr[i] = random.nextInt(UpperBound);
        }
        System.out.println("待排序数组: " + Arrays.toString(arr));
        int[] ans = Counting_Sort(arr);
        System.out.println("已排序数组: " + Arrays.toString(ans));
    }

    /**
     * Accepts an array to be sorted and returns a sorted array. In this method,
     * two integer arrays should be established. One of which is a counting array,
     * the other one is an array to be returned. This method comprises three loops.
     * <ul>
     *     <li>The first <em>For-Loop</em> is to figure out the times of each unique
     *     element in the specified array.</li>
     *     <li>The second <em>For-Loop</em> works on the counting array, in the
     *     range from 1 to length - 1, where current element changes to the value
     *     of summation of current element and previous element.</li>
     *     <li>The third <em>For-Loop</em> works also on the specified array. For
     *     each current element in specified array, find how many elements is less
     *     current element, which means the end index of current element in specified
     *     array. And then assign the result array using the end index with current
     *     element.</li>
     * </ul>
     * This method is stable, obviously. And operations are linear. The total cost
     * of time for this method is theta(k + n), where k is the upper bound of elements
     * of specified array. In general, the complexity of time is theta(n). In this
     * sort, there exists some assumptions that each element in the specified array
     * locates in the range [0, k].
     *
     * @param arr specified array to be sorted
     * @return a sorted array
     */
    public static int[] Counting_Sort(@NotNull int[] arr) {
        int[] counts = new int[UpperBound + 1], ans = new int[arr.length];
        for (int i = 0; i < arr.length; i++) {
            counts[arr[i]]++;
        }
        for (int i = 1; i < counts.length; i++) {
            counts[i] = counts[i] + counts[i - 1];
        }
        for (int i = arr.length - 1; i >= 0; i--) {
            ans[counts[arr[i]]-- - 1] = arr[i];
        }
        return ans;
    }

}

运行结果如下:

待排序数组: [27, 3, 7, 21, 5, 22, 1, 3, 33, 9, 36, 17, 26, 39, 39, 9, 22, 21, 30, 13]
已排序数组: [1, 3, 3, 5, 7, 9, 9, 13, 17, 21, 21, 22, 22, 26, 27, 30, 33, 36, 39, 39]
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值