Counting Sort
前言
In computer science, counting sort is an algorithm for sorting a collection of objects according to keys that are small integers; that is, it is an integer sorting algorithm. It operates by counting the number of objects that have each distinct key value, and using arithmetic on those counts to determine the positions of each key value in the output sequence. Its running time is linear in the number of items and the difference between the maximum and minimum key values, so it is only suitable for direct use in situations where the variation in keys is not significantly greater than the number of items. However, it is often used as a subroutine in another sorting algorithm, radix sort, that can handle larger keys more efficiently.——摘自 Wikipedia Counting sort
从上述的文字中我们可以了解到以下的信息:
- 计数排序是一个整数排序算法
- 计数排序的时间复杂度是线性的
- 计数排序通常只适用于键值范围不是远大于元素数量的情况
- 基数排序可以解决3不适用的范围
思想
这里以数组 [2,5,3,1,2,3,1,3],作为测试数据,这里很容易看出来键值范围为 0~5(k).对于数组中的元素来说,如果我知道有多少元素不大于该元素的话,基于这样的前提条件下,就可以知道排序后该元素在数组中的位置.
过程
基于上述的思路,我们尝试去模拟一下整个过程:
代码
1 | public class CountSort { |
输出结果如下:
1 | 1 1 2 2 3 3 3 5 |
优化
仔细观察一下数组C,我们可以发现对于数组C,其索引为0的元素对应的值为0,有没有可能将数组C的所有元素全部左移一位呢.从而减少开销呢?完全可以的,我们可以将长度由原来的k+1,优化为max-min+1,其中max,min分别对应为A的最大值和最小值.
1 | public class CountSort { |