# 基数排序

基数排序是一种非比较排序算法，它把整数或字符串的键拆成若干片段（称为位或块）来处理，每处理一个片段就用一个稳定的子程序排序对整个集合排一遍，从最低有效位开始。由于它从不将两个键相互比较，因此避开了适用于比较排序的 Ω(n log n) 下界，并且在位数固定时可以在线性时间内运行。<sup>[1](https://xlinux.nist.gov/dads/HTML/radixsort.html)</sup><sup> • </sup><sup>[2](https://franckc.folk.ntnu.no/idata2302/2023/recursion/radix_sort.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 方法 | 一种多趟分配排序：每一趟按照键的某一部分将元素放入桶中，LSD 变体从最低有效部分开始，然后按顺序收集。<sup>[1](https://xlinux.nist.gov/dads/HTML/radixsort.html)</sup> |
| 稳定性要求 | 每一位的排序必须是稳定的，这样后续的趟不会推翻先前各趟建立的顺序；计数排序是常用的选择。<sup>[3](https://www.cs.cornell.edu/courses/cs2112/2018fa/lectures/lecture.html?id=radix_sort)</sup> |
| 时间复杂度 | 每趟使用 Θ(n + k) 的计数排序时，基数排序的运行时间为 Θ(d(n + k))，其中 d 是位数，n 是元素个数，k 是每一位的类别数。<sup>[4](https://www2.hawaii.edu/~suthers/courses/ics311f20/Notes/Topic-10.html)</sup> |
| 空间复杂度 | O(n + k)，与每趟使用的计数排序相同。<sup>[2](https://franckc.folk.ntnu.no/idata2302/2023/recursion/radix_sort.html)</sup> |
| 稳定性 | 基数排序本身是稳定的。<sup>[2](https://franckc.folk.ntnu.no/idata2302/2023/recursion/radix_sort.html)</sup> |
| 起源 | 逐位卡片排序曾被赫尔曼·何乐礼（Herman Hollerith）的机器用于 1890 年美国人口普查；计算机版本可追溯到哈罗德·H·苏厄德（Harold H. Seward）1954 年在麻省理工学院的硕士论文。<sup>[5](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2008/0c8bb24ef91668bfe85f1eaee80e72a0_lec11.pdf)</sup><sup> • </sup><sup>[6](https://kolegite.com/EE_library/books_and_lectures/%D0%9F%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%B8%D1%80%D0%B0%D0%BD%D0%B5/The_Art_of_Computer_Programming/The%20Art%20of%20Computer%20Programming%20Volume%203%20Sorting%20and%20Searching%20%28Donald%20E.%20Knuth%29%20%28z-lib.org%29.pdf)</sup> |
| 现代应用 | 基数排序是 GPU 上的标准高性能原语；Onesweep 算法在 NVIDIA A100 上对 2.56 亿个随机 32 位键排序时达到 29.4 GKey/s。<sup>[7](https://arxiv.org/abs/2206.01784)</sup> |

## 工作原理

该算法将每个键视为一个片段序列 C₁、C₂、……、Cₙ，例如整数的十进制数字或字符串的字符，并产生字典序：先按 C₁ 排，C₁ 值相同者再按 C₂ 排，依此类推。<sup>[3](https://www.cs.cornell.edu/courses/cs2112/2018fa/lectures/lecture.html?id=radix_sort)</sup> 最低位优先（LSD）变体实现这一点的方式是：先对整个数组按 Cₙ 排序，再按 Cₙ₋₁ 排序，依此类推直到 C₁，每一趟都使用稳定排序。<sup>[3](https://www.cs.cornell.edu/courses/cs2112/2018fa/lectures/lecture.html?id=radix_sort)</sup>

稳定性是支撑这一算法的关键性质。当相等元素保持其原有的相对顺序时，排序就是稳定的。<sup>[3](https://www.cs.cornell.edu/courses/cs2112/2018fa/lectures/lecture.html?id=radix_sort)</sup> 在按后续某一位排序之后，稳定排序会在该位值相同的元素之间保留先前各趟建立的顺序；不稳定的按位排序会破坏这些工作并产生错误结果。<sup>[3](https://www.cs.cornell.edu/courses/cs2112/2018fa/lectures/lecture.html?id=radix_sort)</sup><sup> • </sup><sup>[5](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2008/0c8bb24ef91668bfe85f1eaee80e72a0_lec11.pdf)</sup> [计数排序](https://www.edgepedia.org/zh/counting-sort)对 k 个类别运行时间为 O(n + k)，是稳定的，也是常用的子程序；用先进先出队列处理的桶排序是另一种稳定选择。<sup>[2](https://franckc.folk.ntnu.no/idata2302/2023/recursion/radix_sort.html)</sup><sup> • </sup><sup>[4](https://www2.hawaii.edu/~suthers/courses/ics311f20/Notes/Topic-10.html)</sup><sup> • </sup><sup>[8](https://cs.nyu.edu/~gottlieb/courses/2000s/2002-03-fall/alg/lectures/lecture-23.html)</sup>

美国国家标准与技术研究院的《算法与数据结构词典》用键 34、12、42、32、44、41、34、11、32、23 说明了收集步骤：按个位排过一趟后，各桶中分别是 41 11、12 42 32 32、23 和 34 44 34；按顺序收集它们再按十位重新分配，得到有序序列 11 12 23 32 32 34 34 41 42 44。<sup>[1](https://xlinux.nist.gov/dads/HTML/radixsort.html)</sup> 该词典还指出，基数排序在链表上效果特别好，因为一趟只需把各桶的链表缝合起来。<sup>[1](https://xlinux.nist.gov/dads/HTML/radixsort.html)</sup>

## 复杂度及其为何能突破比较下界

每一种基于比较的排序在最坏情况下都需要 Ω(n log n) 时间，因为区分 n! 种排列的决策树高度至少为 log₂ n!。<sup>[2](https://franckc.folk.ntnu.no/idata2302/2023/recursion/radix_sort.html)</sup>

以计数排序作为子程序时，每趟花费 Θ(n + k)，d 趟花费 Θ(d(n + k))。<sup>[4](https://www2.hawaii.edu/~suthers/courses/ics311f20/Notes/Topic-10.html)</sup> 对于 n 个各 b 位的字，一趟计数排序花费 Θ(n + 2<sup>b</sup>)；麻省理工学院 6.006 课程的分析表明，选择位数 r 使两项平衡，取 r = lg n，可使 b/r 趟的总代价最小。<sup>[5](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2008/0c8bb24ef91668bfe85f1eaee80e72a0_lec11.pdf)</sup> 空间为 O(n + k)，不超过计数排序本身所需。<sup>[2](https://franckc.folk.ntnu.no/idata2302/2023/recursion/radix_sort.html)</sup>

## 历史

逐位处理的思想早于计算机。赫尔曼·何乐礼为 1890 年美国人口普查制造的卡片分拣机，一次检查每张穿孔卡片的一列，将卡片分入 10 个格子之一，然后收集各格；最低位优先策略更受青睐，因为卡片可以直接收集成一叠，而最高位优先策略会留下大量中间堆需要搁置。<sup>[5](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2008/0c8bb24ef91668bfe85f1eaee80e72a0_lec11.pdf)</sup> 何乐礼的成果为他赢得了处理 1890 年人口普查的合同，其技术的改进版本一直在人口普查局使用，直到 20 世纪 50 年代被计算机取代。<sup>[9](https://www.census.gov/about/history/bureau-history/census-innovations/technology/hollerith-machine.html)</sup>

在计算机内部，基数排序最初被拒绝，因为它似乎需要 (M + 1)N 个记录空间来处理 M 个堆和 N 条记录。哈罗德·H·苏厄德在其 1954 年麻省理工学院硕士论文中证明，对数据做一次预备性的计数遍历可以精确分配各堆，因此只需 2N 个记录区域和 M 个计数字段就能达到同样的效果；高德纳（Donald E. Knuth）在其《计算机程序设计艺术》中涵盖了这段历史，也将计数排序的发明以及将其与基数排序结合归功于苏厄德。<sup>[6](https://kolegite.com/EE_library/books_and_lectures/%D0%9F%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%B8%D1%80%D0%B0%D0%BD%D0%B5/The_Art_of_Computer_Programming/The%20Art%20of%20Computer%20Programming%20Volume%203%20Sorting%20and%20Searching%20%28Donald%20E.%20Knuth%29%20%28z-lib.org%29.pdf)</sup><sup> • </sup><sup>[10](https://bobson.ludost.net/books/algo/book6/chap09.htm)</sup> 手工进行 LSD 基数排序似乎是机械卡片分拣机操作员广泛使用的一种民间算法。<sup>[10](https://bobson.ludost.net/books/algo/book6/chap09.htm)</sup> 一种相关但不同的方法——基数交换排序，一种原地二进制技术——由系统开发公司的保罗·希尔德布兰特（Paul Hildebrandt）和哈罗德·伊斯比茨（Harold Isbitz）于 1959 年发表在《ACM 杂志》上。<sup>[11](https://doi.org/10.1145/320964.320972)</sup>

## 实际性能

渐近线性并不能保证最快的实际运行时间。1999 年一项关于缓存行为的研究发现，尽管这种线性时间算法的指令数极低，但基数排序相对较差的缓存局部性使其在该硬件上总体上比精心调优的比较排序更慢。<sup>[12](https://www.sciencedirect.com/science/article/pii/S0196677498909853)</sup> 后来的工作量化了代价：对大于 L1 或 L2 缓存的数组进行随机访问，相对于顺序访问可能带来 40 到 70 倍的惩罚因子，而基数排序的变体直接暴露在这种效应之下。<sup>[13](https://www.researchgate.net/publication/4274165_A_Model_for_the_Effect_of_Caching_on_Algorithmic_Efficiency_in_Radix_based_Sorting)</sup> 反过来，对于短小恒定长度的字符串等特殊数据，基数排序在实践中往往比[快速排序](https://www.edgepedia.org/zh/quicksort)快得多。<sup>[14](https://www.cs.utexas.edu/~djimenez/utsa/cs3343/lecture10.html)</sup> 一项对桶排序和基数排序的实证比较发现，桶排序在测试输入上更快，但在大多数情况下内存使用更重。<sup>[15](https://arxiv.org/abs/1206.3511)</sup>

**并行与 GPU 基数排序。** 该算法固定的、与数据无关的各趟处理暴露了细粒度并行性，这使其成为基于并行扫描操作构建的标准 GPU 原语。NVIDIA 2008 年的报告描述了一种 CUDA 基数排序，它比基于图形 API 的 GPUSort 快达 4 倍，比 8 核 2.33 GHz Intel Core2 Xeon 上的同类例程快达 3.5 倍。<sup>[16](https://www.nvidia.com/docs/io/67073/nvr-2008-001.pdf)</sup> 2016 年的一种混合设计将内存传输近乎减半，在短短 50 毫秒内对 2 GB 的八字节记录完成排序，对于均匀分布，性能达到当时最先进的 GPU 基数排序的 2.32 倍。<sup>[17](https://arxiv.org/abs/1611.01137)</sup> 2022 年的 Onesweep 算法使用单趟前缀和将每次数字分桶迭代减少到约 2n 次全局读/写操作，而当代实现约为 3n 次，在 [NVIDIA A100](https://www.edgepedia.org/zh/nvidia-a100) 上对 2.56 亿个随机 32 位键达到 29.4 GKey/s，约为 CUB 库 LSD 基数排序吞吐量的 1.5 倍。<sup>[7](https://arxiv.org/abs/2206.01784)</sup>

## 变体

通常区分两个家族。上文描述的 LSD 基数排序从最低有效位开始运行，需要稳定的子程序。<sup>[18](https://www.youtube.com/watch?v=M2cCN7Gzl9U)</sup><sup> • </sup><sup>[19](https://www.researchgate.net/publication/220195796_Formulation_and_analysis_of_in-place_MSD_radix_sort_algorithms)</sup> MSD（最高位优先）基数排序则对首位创建的桶进行递归，适合字符串等变长键；已有研究探讨原地 MSD 形式以避免辅助数组。<sup>[19](https://www.researchgate.net/publication/220195796_Formulation_and_analysis_of_in-place_MSD_radix_sort_algorithms)</sup> 美国国家标准与技术研究院词典列出的相关分配排序包括美国国旗排序（一种自顶向下的基数排序）和邮递员排序。<sup>[1](https://xlinux.nist.gov/dads/HTML/radixsort.html)</sup> 关于空间代价的研究仍在继续：2025 年的变体 Radsort 仅用 O(√n) 的额外空间进行排序，对于大于约 2 MiB 的数组，其性能优于传统的非原地 LSD 基数排序。<sup>[20](https://arxiv.org/abs/2607.05302)</sup>

## References

1. [Paul E. Black, "radix sort", Dictionary of Algorithms and Data Structures, NIST, entry modified 21 April 2022.](https://xlinux.nist.gov/dads/HTML/radixsort.html)
2. [Franck Cassez, "Sorting Without Comparing", IDATA 2302 Algorithms and Data Structures course notes, NTNU.](https://franckc.folk.ntnu.no/idata2302/2023/recursion/radix_sort.html)
3. [CS 2112/ENGRD 2112 Fall 2018 lecture notes on radix sort, Cornell University.](https://www.cs.cornell.edu/courses/cs2112/2018fa/lectures/lecture.html?id=radix_sort)
4. [Daniel Suthers, ICS 311 #10: Quicksort, Theoretical Limits, and O(n) Sorts, University of Hawaiʻi.](https://www2.hawaii.edu/~suthers/courses/ics311f20/Notes/Topic-10.html)
5. [MIT 6.006 Introduction to Algorithms, Spring 2008, Lecture 11: Stable Sorting, Radix Sort, MIT OpenCourseWare.](https://ocw.mit.edu/courses/6-006-introduction-to-algorithms-spring-2008/0c8bb24ef91668bfe85f1eaee80e72a0_lec11.pdf)
6. [Donald E. Knuth, The Art of Computer Programming, Volume 3: Sorting and Searching.](https://kolegite.com/EE_library/books_and_lectures/%D0%9F%D1%80%D0%BE%D0%B3%D1%80%D0%B0%D0%BC%D0%B8%D1%80%D0%B0%D0%BD%D0%B5/The_Art_of_Computer_Programming/The%20Art%20of%20Computer%20Programming%20Volume%203%20Sorting%20and%20Searching%20%28Donald%20E.%20Knuth%29%20%28z-lib.org%29.pdf)
7. [Andy Adinets, Duane Merrill (2022). Onesweep: A Faster Least Significant Digit Radix Sort for GPUs. arXiv.](https://arxiv.org/abs/2206.01784)
8. [Joel Gottlieb, Basic Algorithms Lecture #23, New York University.](https://cs.nyu.edu/~gottlieb/courses/2000s/2002-03-fall/alg/lectures/lecture-23.html)
9. [The Hollerith Machine, U.S. Census Bureau history.](https://www.census.gov/about/history/bureau-history/census-innovations/technology/hollerith-machine.html)
10. [Introduction to Algorithms, Chapter 9: Sorting in Linear Time.](https://bobson.ludost.net/books/algo/book6/chap09.htm)
11. [Paul Hildebrandt, Harold Isbitz (1959). Radix Exchange, An Internal Sorting Method for Digital Computers. Journal of the ACM 6(2), 156–163.](https://doi.org/10.1145/320964.320972)
12. [Peter A. LaMarca, Richard E. Ladner (1999). The Influence of Caches on the Performance of Sorting. Journal of Algorithms 31(1), 66–104.](https://www.sciencedirect.com/science/article/pii/S0196677498909853)
13. [A Model for the Effect of Caching on Algorithmic Efficiency in Radix based Sorting (2007).](https://www.researchgate.net/publication/4274165_A_Model_for_the_Effect_of_Caching_on_Algorithmic_Efficiency_in_Radix_based_Sorting)
14. [David Jimenez, Analysis of Algorithms Lecture 10, University of Texas at San Antonio.](https://www.cs.utexas.edu/~djimenez/utsa/cs3343/lecture10.html)
15. [Comparison of Bucket Sort and RADIX Sort. arXiv.](https://arxiv.org/abs/1206.3511)
16. [Nadathur Satish, Mark Harris, Michael Garland (2008). Designing Efficient Sorting Algorithms for Manycore GPUs. NVIDIA Technical Report NVR-2008-001.](https://www.nvidia.com/docs/io/67073/nvr-2008-001.pdf)
17. [Elias Stehle, Hans-Arno Jacobsen (2016). A Memory Bandwidth-Efficient Hybrid Radix Sort on GPUs. arXiv.](https://arxiv.org/abs/1611.01137)
18. [Quoc Dat Phung, Radix Sort Algorithm: A Step-by-Step Visualization (video).](https://www.youtube.com/watch?v=M2cCN7Gzl9U)
19. [Formulation and analysis of in-place MSD radix sort algorithms (2005).](https://www.researchgate.net/publication/220195796_Formulation_and_analysis_of_in-place_MSD_radix_sort_algorithms)
20. [Parallel O(√n) Overhead LSD Radix Sort (2025). arXiv.](https://arxiv.org/abs/2607.05302)

---
*Initially written Sep 17, 2026 · Reviewed: Sep 30, 2026; Oct 15, 2026 · Edited: Oct 15, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 人工智能与数据 › Algorithms and computational methods › Sorting, searching, and selection › Non-comparison and distribution sorting

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/radix-sort

English original: https://www.edgepedia.org/radix-sort
