# 双调排序器

双调排序器（bitonic sorter）是肯尼斯·E·巴彻（Kenneth E. Batcher）设计的一种排序网络，能对任意双调序列排序——所谓双调序列，是指先单调递增、再单调递减（或反之）的序列；通过递归地构造并归并这类序列，它以规则且与数据无关的比较/交换操作结构，在 O(log²n) 并行时间内完成对任意输入的排序。<sup>[1](https://www.cs.kent.edu/~batcher/sort.pdf)</sup><sup> • </sup><sup>[2](https://citeseerx.ist.psu.edu/document?doi=37b60370a200902db33c1e1136350e7afa8ea862&repid=rep1&type=pdf)</sup><sup> • </sup><sup>[3](https://developer.nvidia.com/gpugems/gpugems2/part-vi-simulation-and-numerical-algorithms/chapter-46-improved-gpu-sorting)</sup>

| 关键事实 | 详情 |
|---|---|
| 起源 | K. E. 巴彻，《排序网络及其应用》，AFIPS 春季联合计算机会议，1968 年，第 307–314 页；更早的内部版本以固特异航空航天公司（Goodyear Aerospace）报告 GER-11759 和 GER-11869 的形式于 1964 年问世<sup>[1](https://www.cs.kent.edu/~batcher/sort.pdf)</sup> |
| 并行时间 | O(log²n)：log n 个阶段，每阶段至多 log n 趟，每阶段并行执行 n 次比较；n 个输入的网路有 log₂ n · ((log₂ n)+1)/2 层<sup>[3](https://developer.nvidia.com/gpugems/gpugems2/part-vi-simulation-and-numerical-algorithms/chapter-46-improved-gpu-sorting)</sup><sup> • </sup><sup>[4](https://proceedings.mlr.press/v139/petersen21a/petersen21a.pdf)</sup> |
| 工作量 | O(n log²n) 次比较，相比之下归并排序为 O(n log n)，基数排序为 O(n)<sup>[5](https://www.cs.umd.edu/class/spring2021/cmsc714/readings/Satish-sorting.pdf)</sup> |
| 数据无关性 | 比较模式事先固定，不依赖输入值，因此同一阶段内的所有比较可并发执行，不存在依赖数据的分支<sup>[3](https://developer.nvidia.com/gpugems/gpugems2/part-vi-simulation-and-numerical-algorithms/chapter-46-improved-gpu-sorting)</sup> |
| GPU 上的地位 | 2011 年的一项 CUDA 原地实现在其测试中是针对 32 位键的最快基于比较的 GPU 排序，仅在元素数超过 2^23 时被基数排序超越，而在 64 位键上胜过所有被测算法<sup>[6](https://doi.org/10.1002/cpe.1686)</sup> |
| 稳定性 | 不是稳定排序；相等的键可能被重排<sup>[7](https://forums.developer.nvidia.com/t/cuda-toolkit-10-0-sortingnetworks-sample/65502)</sup> |
| 硬件应用 | 文献称，数十年来在 GPU 和 FPGA 上的许多硬件排序实现中，它是最常用的方法<sup>[8](https://pure.ku.edu.kw/en/publications/competition-style-sorting-networks-csn-a-framework-for-hardware-b/)</sup> |

## 网络的工作原理

该算法建立在双调序列之上。若一个序列先递增至某一点然后递减，或先递减然后递增，它就是双调的；等价地说，它是一条单调递增序列与一条单调递减序列的拼接。<sup>[2](https://citeseerx.ist.psu.edu/document?doi=37b60370a200902db33c1e1136350e7afa8ea862&repid=rep1&type=pdf)</sup><sup> • </sup><sup>[9](https://www.cs.auckland.ac.nz/~cthombor/Pubs/cmu-cs-80-140/CMU-CS-80-140.pdf)</sup> 双调排序器（巴彻自己的叫法，它也被称为双调归并器）是一种能对每个双调输入向量排序的网络。<sup>[2](https://citeseerx.ist.psu.edu/document?doi=37b60370a200902db33c1e1136350e7afa8ea862&repid=rep1&type=pdf)</sup><sup> • </sup><sup>[9](https://www.cs.auckland.ac.nz/~cthombor/Pubs/cmu-cs-80-140/CMU-CS-80-140.pdf)</sup>

完整的排序网络遵循分治模式。它从单个元素出发，递归地把成对的单调序列组合成双调序列，再把每个双调序列归并为单条单调序列，最终得到一张有序表。<sup>[4](https://proceedings.mlr.press/v139/petersen21a/petersen21a.pdf)</sup> 在等价的迭代视角下，网络先构造出键的一条递增子序列和一条递减子序列，二者共同构成一个双调序列，然后将其归并；从 n/2 个含两项的序列开始，如此反复，直到只剩一条有序序列，共得到 log(n) 个排序阶段，其中第 i 阶段执行 i 趟。<sup>[3](https://developer.nvidia.com/gpugems/gpugems2/part-vi-simulation-and-numerical-algorithms/chapter-46-improved-gpu-sorting)</sup> 每个阶段并行执行 n 次比较与交换，因此由 n 个处理器组成的阵列可在 O(log²n) 步内排好 n 个元素。<sup>[10](https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-37-toolkit-computation-gpus)</sup>

有两个结构性质在实践中很重要。其拓扑结构还类似快速[傅里叶变换](https://www.edgepedia.org/zh/fourier-transform)，巴彻本人在 1968 年的论文中就指出了这一点。<sup>[1](https://www.cs.kent.edu/~batcher/sort.pdf)</sup>

## 起源

巴彻在固特异航空航天公司发展出这一方法，题为《一种新的内部排序方法》（GER-11759）和《双调排序》（GER-11869）的内部报告于 1964 年问世；他还在 1968 年 AFIPS 春季联合计算机会议上宣读了《排序网络及其应用》。<sup>[1](https://www.cs.kent.edu/~batcher/sort.pdf)</sup> 该论文把排序网络视为将大型计算系统各部件灵活连接起来的一种方式，其应用包括带缓冲的交换网络、多路访问存储器、多路访问内容可寻址存储器以及多处理器。<sup>[1](https://www.cs.kent.edu/~batcher/sort.pdf)</sup> “双调”（bitonic）一词由巴彻首创，后来的文献沿用了他的术语。<sup>[2](https://citeseerx.ist.psu.edu/document?doi=37b60370a200902db33c1e1136350e7afa8ea862&repid=rep1&type=pdf)</sup>

## 与其他并行排序的性能比较

O(log²n) 的深度并非渐近最优。阿伊泰（Ajtai）、科姆洛什（Komlós）和塞迈雷迪（Szemerédi）的 AKS 排序网络以 O(n log n) 次操作在 O(log n) 并行时间内完成排序，但其复杂度常数未知；按乐观估计，只有当 n ≫ 10^80 时它才比双调排序更快，因此双调排序仍是实用上首选的网络。<sup>[4](https://proceedings.mlr.press/v139/petersen21a/petersen21a.pdf)</sup> 遗传与演化搜索已针对特定的小 n 找到了略优的最优网络，但它们缺乏双调网络那种简单而规则的结构。<sup>[4](https://proceedings.mlr.press/v139/petersen21a/petersen21a.pdf)</sup>

实测结果与理论相符。在一个 Connection Machine CM-2 项目中，研究者实现了巴彻的双调排序、一种并行基数排序，以及一种类似赖夫（Reif）与瓦利安特（Valiant）的闪电排序（flashsort）的样本排序；双调排序在小数据集上是三者中最快的，也最省空间，而样本排序在大数据集上最快。<sup>[11](https://www.cs.cmu.edu/~bmm/BlellochLMPSZ94.html)</sup> 在 GPU 上，GPUSort 所用的双调排序的工作量为 O(n log²n)，而归并排序为 O(n log n)，基数排序为 O(n)；萨蒂什（Satish）、哈里斯（Harris）和加兰（Garland）2009 年的 CUDA 基数排序在测试的最大输入规模上，排序速度是 GPUSort 的 4 倍以上。<sup>[5](https://www.cs.umd.edu/class/spring2021/cmsc714/readings/Satish-sorting.pdf)</sup> 基尔克里斯蒂安-阿尔布雷希特大学（Christian-Albrechts-University Kiel）的哈根·彼得斯（Hagen Peters）、奥勒·舒尔茨-希尔德布兰特（Ole Schulz-Hildebrandt）和诺贝特·卢滕贝格（Norbert Luttenberger）在 2011 年的一项研究中提出了巴彻双调排序网络的高性能原地 CUDA 实现，它把比较/交换操作分配给各线程，以减少低效的全局内存访问并高效利用共享内存；在他们针对 64 位键的测试中，它胜过所有其他算法，在 32 位键上则是最快的基于比较的 GPU 排序算法，只有序列长度超过 2^23 时才被非比较类的基数排序超越。<sup>[6](https://doi.org/10.1002/cpe.1686)</sup> 一个带有两项实现层面优化的 2015 年 CUDA 实现在 32 位随机整数上达到了 CPU 快速排序的近 20 倍速度，在数组规模约为 2^16 时加速比高达 30。<sup>[12](https://arxiv.org/abs/1506.01446)</sup>

## GPU 与硬件实现

双调归并排序很早就被移植到图形硬件上，因为它契合 GPU 受限的编程模型：每次渲染通道执行一个并行比较阶段，由片段程序根据当前阶段编号和步数算出要比较的是哪两个元素。<sup>[10](https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-37-toolkit-computation-gpus)</sup> NVIDIA 的《GPU Gems 2》描述了一种实现：每个片段打包两对键/索引，把每个阶段的最后一趟折叠进前一趟，最终的数据读取次数与 CPU 实现相同；在 425 MHz 的 GeForce 6800 Ultra 上，它能对 16 位键/16 位索引对进行排序。<sup>[3](https://developer.nvidia.com/gpugems/gpugems2/part-vi-simulation-and-numerical-algorithms/chapter-46-improved-gpu-sorting)</sup> 同一文献指出了一个局限：中间阶段并不维持全局有序的序列，因为它们总是产生由一条递增和一条递减局部序列构成的双调对，所以每个阶段至少有一半数据被彻底重排，各趟处理也无法分散到多帧中完成。<sup>[3](https://developer.nvidia.com/gpugems/gpugems2/part-vi-simulation-and-numerical-algorithms/chapter-46-improved-gpu-sorting)</sup>

NVIDIA 的 CUDA 示例集长期以来一直附带一个 sortingNetworks 示例，实现了双调排序和奇偶归并排序；尽管其渐近复杂度不如归并排序或基数排序，它仍被描述为对短小到中等规模的键值数组对进行批量排序时的首选；双调实现只支持长度为 2 的幂的数组。<sup>[13](https://docs.nvidia.com/cuda/archive/9.1/pdf/CUDA_Samples.pdf)</sup><sup> • </sup><sup>[14](https://github.com/ndd314/cuda_examples/blob/master/6_Advanced/sortingNetworks/bitonicSort.cu)</sup> 该网络也不是稳定的，因此相等的键可能改变相对次序。<sup>[7](https://forums.developer.nvidia.com/t/cuda-toolkit-10-0-sortingnetworks-sample/65502)</sup>

在硬件中，这种固定结构可直接映射为电路。一项分类学研究指出，双调排序可以不用多级网络，而构建为比较器数量少得多的循环网络，例如由移位寄存器和混洗链路连接的单级比较器阵列。<sup>[15](https://calhoun.nps.edu/bitstream/handle/10945/46758/Hsiao_A_Taxonomy_1984.pdf?sequence=1)</sup>

## 变体与其他用途

若干改进降低了基本网络的成本。2015 年的一项研究将多步双调排序和自适应双调排序与标准版本做了比较，该研究还调整了实现，使其能对任意长度的输入序列排序，覆盖 32 位与 64 位数字及键值对的六种输入分布。<sup>[16](https://arxiv.org/abs/1511.03404)</sup> 在硬件方面，混合设计用折叠双调归并网络取代归并排序网络的最后几个阶段，以降低延迟和内存占用。

该网络在机器学习中也找到了用武之地。由于其比较/交换操作可以松弛为可微操作，双调排序网络可用作可微分排序与排名层：彼得森（Petersen）、博格尔特（Borgelt）、屈内（Kuehne）和多伊森（Deussen）在 ICML 2021 上表明，松弛后的奇偶网络与双调网络优于现有的排序操作松弛方法，其中双调网络在多达 1024 个元素的输入集上实现了稳定训练。<sup>[4](https://proceedings.mlr.press/v139/petersen21a/petersen21a.pdf)</sup> 相关工作也在排序之外构建于该网络之上：利姆（Lim）与赖特（Wright）基于双调排序网络，为难解的排列问题提出了一种坐标下降算法。<sup>[4](https://proceedings.mlr.press/v139/petersen21a/petersen21a.pdf)</sup>

## References

1. [K. E. Batcher (1968). Sorting networks and their applications. AFIPS Spring Joint Computer Conference, pp. 307–314.](https://www.cs.kent.edu/~batcher/sort.pdf)
2. [M. Mihaljević et al. area survey including bitonic sorting definitions, Theoretical Computer Science 410 (2009) 1369–1376.](https://citeseerx.ist.psu.edu/document?doi=37b60370a200902db33c1e1136350e7afa8ea862&repid=rep1&type=pdf)
3. [Chapter 46. Improved GPU Sorting, NVIDIA GPU Gems 2.](https://developer.nvidia.com/gpugems/gpugems2/part-vi-simulation-and-numerical-algorithms/chapter-46-improved-gpu-sorting)
4. [F. Petersen, C. Borgelt, H. Kuehne, O. Deussen (2021). Differentiable Sorting Networks for Scalable Sorting and Ranking Supervision. PMLR 139:8546–8555.](https://proceedings.mlr.press/v139/petersen21a/petersen21a.pdf)
5. [N. Satish, M. Harris, M. Garland (2009). Designing efficient sorting algorithms for manycore GPUs. IPDPS 2009.](https://www.cs.umd.edu/class/spring2021/cmsc714/readings/Satish-sorting.pdf)
6. [H. Peters, O. Schulz-Hildebrandt, N. Luttenberger (2011). Fast in-place, comparison-based sorting with CUDA: a study with bitonic sort. Concurrency and Computation: Practice and Experience 23:681–693.](https://doi.org/10.1002/cpe.1686)
7. [CUDA Toolkit 10.0 sortingNetworks sample, NVIDIA Developer Forums.](https://forums.developer.nvidia.com/t/cuda-toolkit-10-0-sortingnetworks-sample/65502)
8. [A. Fairouz, J. M. Aljuraidan, A. Mohammed (2025). Competition-Style Sorting Networks (CSN). IEEE Transactions on Computers 74(12):4109–4122.](https://pure.ku.edu.kw/en/publications/competition-style-sorting-networks-csn-a-framework-for-hardware-b/)
9. [CMU-CS-80-140, on bitonic sequences and Batcher's terminology.](https://www.cs.auckland.ac.nz/~cthombor/Pubs/cmu-cs-80-140/CMU-CS-80-140.pdf)
10. [Chapter 37. A Toolkit for Computation on GPUs, NVIDIA GPU Gems.](https://developer.nvidia.com/gpugems/gpugems/part-vi-beyond-triangles/chapter-37-toolkit-computation-gpus)
11. [G. E. Blelloch et al. An experimental analysis of parallel sorting algorithms. Theory of Computing Systems 31:135–167 (1998).](https://www.cs.cmu.edu/~bmm/BlellochLMPSZ94.html)
12. [Q. Mu, L. Cui, Y. Song (2015). The implementation and optimization of Bitonic sort algorithm based on CUDA. arXiv:1506.01446.](https://arxiv.org/abs/1506.01446)
13. [CUDA Samples documentation (archive 9.1), sortingNetworks sample.](https://docs.nvidia.com/cuda/archive/9.1/pdf/CUDA_Samples.pdf)
14. [bitonicSort.cu, CUDA sortingNetworks sample source.](https://github.com/ndd314/cuda_examples/blob/master/6_Advanced/sortingNetworks/bitonicSort.cu)
15. [P.-C. Hsiao (1984). A taxonomy of parallel sorting. Naval Postgraduate School.](https://calhoun.nps.edu/bitstream/handle/10945/46758/Hsiao_A_Taxonomy_1984.pdf?sequence=1)
16. [D. Bozidar, T. Dobravec (2015). Comparison of parallel sorting algorithms. arXiv:1511.03404.](https://arxiv.org/abs/1511.03404)

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

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

Copyright 2026 Edgepedia

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

Source: https://www.edgepedia.org/zh/bitonic-sorter

English original: https://www.edgepedia.org/bitonic-sorter
