跳到主要内容

资源 · 指南或文章

Counting Sort

适用于小范围整数键的计数排序,包括它为什么能绕开比较排序的下界,以及它与基数排序和桶排序的关系。

审核日期

打开原站: 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. Because counting sort uses key values as indexes into an array, it is not a comparison sort, and the Ω(n log n) lower bound for comparison sorting does not apply to it. Bucket sort may be used for many of the same tasks as counting sort, with a similar time analysis; however, compared to counting sort, bucket sort requires linked lists, dynamic arrays or a large amount of preallocated memory to hold the sets of items within each bucket, whereas counting…

PrepKit 概览

本概览由 PrepKit 编辑根据源代码仓库撰写并审核,并非资源本身的文字。

适合人群
来源未说明适用级别
形式
参考资料

涵盖内容

  • 为什么它不是比较排序,以及键的范围何时使它适用。
  • 三个步骤:统计出现次数、计算累计计数、把元素放入输出数组。
  • 复杂度表:时间和内存均为 n + r,稳定,其中 r 为最大数。

使用建议

这是 PrepKit 的学习建议,并非来源的说法。

手动把 [4, 2, 2, 8, 3, 3, 1] 走完全部三个步骤完成排序,然后编写代码,并保持其稳定性。

范围与局限

步骤插图是托管在外部博客上的图片。没有处理负数,也没有练习。附有葡萄牙文版本的链接。

获取方式与形式

GitHub 上 trekhleb/javascript-algorithms(MIT 许可)中的免费英文 README。

PrepKit 于 2026年10月2日 审核。 资源来源提交: 85293e3e2b88

本概览的依据
  • src/algorithms/sorting/counting-sort/README.md

收录此条目的页面

订阅 PrepKit 通讯

可选。使用 PrepKit 并不需要订阅。

我们如何处理你的邮箱