American flag sort


Definition: An efficient, in-place variant of radix sort that distributes items into hundreds of buckets. The first step counts the number of items in each bucket, and the second step computes where each bucket will start in the array. The last step cyclically permutes items to their proper bucket. Since the buckets are in order in the array, there is no collection step. The name comes by analogy with the Dutch national flag problem in the last step: efficiently partition the array into many "stripes". Using some efficiency techniques, it is twice as fast as quicksort for large sets of strings.

See also histogram sort.

Note: This works especially well when sorting a byte at a time, using 256 buckets.

Author: PEB


It is program C (C) in the McIlroy, Bostic, and McIlroy paper.

More information

The flag of the United States of America.

Peter M. McIlroy, Keith Bostic, and M. Douglas McIlroy, Engineering Radix Sort, Computing Systems, 6(1):5-27, 1993.

Go to the Dictionary of Algorithms and Data Structures home page.

If you have suggestions, corrections, or comments, please get in touch with Paul Black.

Entry modified 24 January 2023.
HTML page formatted Tue Jan 24 09:34:32 2023.

Cite this as:
Paul E. Black, "American flag sort", in Dictionary of Algorithms and Data Structures [online], Paul E. Black, ed. 24 January 2023. (accessed TODAY) Available from: