|
10 | 10 | #ifndef _LIBCPP___ALGORITHM_RADIX_SORT_H |
11 | 11 | #define _LIBCPP___ALGORITHM_RADIX_SORT_H |
12 | 12 |
|
| 13 | +// This is an implementation of classic LSD radix sort algorithm, running in linear time and using `O(max(N, M))` |
| 14 | +// additional memory, where `N` is size of an input range, `M` — maximum value of |
| 15 | +// a radix of the sorted integer type. Type of the radix and its maximum value are determined at compile time |
| 16 | +// based on type returned by function `__radix`. The default radix is uint8. |
| 17 | + |
| 18 | +// The algorithm is equivalent to several consecutive calls of counting sort for each |
| 19 | +// radix of the sorted numbers from low to high byte. |
| 20 | +// The algorithm uses a temporary buffer of size equal to size of the input range. Each `i`-th pass |
| 21 | +// of the algorithm sorts values by `i`-th radix and moves values to the temporary buffer (for each even `i`, counted |
| 22 | +// from zero), or moves them back to the initial range (for each odd `i`). It there is only one radix in sorted integers |
| 23 | +// (e.g. int8), than sorted values are placed to the buffer, and then moved back to the initial range. |
| 24 | + |
| 25 | +// The implementation also has several optimizations: |
| 26 | +// - the counters for the counting sort are calculated in one pass for all radices; |
| 27 | +// - if all values of a radix are the same, we do not sort that radix, and just move items to the buffer; |
| 28 | +// - if two consecutive radices satisfies condition above, we do nothing for these two radices. |
| 29 | + |
13 | 30 | #include <__algorithm/copy.h> |
14 | 31 | #include <__algorithm/for_each.h> |
15 | 32 | #include <__bit/countl.h> |
|
0 commit comments