`sort`

is a generic function for which methods can be written,
and `sort.int`

is the internal method which is compatible
with S if only the first three arguments are used.

The default `sort`

method makes use of `order`

for
classed objects, which in turn makes use of the generic function
`xtfrm`

(and can be slow unless a `xtfrm`

method has
been defined or `is.numeric(x)`

is true).

Complex values are sorted first by the real part, then the imaginary
part.

The `"auto"`

method selects `"radix"`

for short (less than
\(2^{31}\) elements) numeric vectors, integer vectors, logical
vectors and factors; otherwise, `"shell"`

.

Except for method `"radix"`

,
the sort order for character vectors will depend on the collating
sequence of the locale in use: see `Comparison`

.
The sort order for factors is the order of their levels (which is
particularly appropriate for ordered factors).

If `partial`

is not `NULL`

, it is taken to contain indices
of elements of the result which are to be placed in their correct
positions in the sorted array by partial sorting. For each of the
result values in a specified position, any values smaller than that
one are guaranteed to have a smaller index in the sorted array and any
values which are greater are guaranteed to have a bigger index in the
sorted array. (This is included for efficiency, and many of the
options are not available for partial sorting. It is only
substantially more efficient if `partial`

has a handful of
elements, and a full sort is done (a Quicksort if possible) if there
are more than 10.) Names are discarded for partial sorting.

Method `"shell"`

uses Shellsort (an \(O(n^{4/3})\) variant from
Sedgewick (1986)). If `x`

has names a stable modification is
used, so ties are not reordered. (This only matters if names are
present.)

Method `"quick"`

uses Singleton (1969)'s implementation of
Hoare's Quicksort method and is only available when `x`

is
numeric (double or integer) and `partial`

is `NULL`

. (For
other types of `x`

Shellsort is used, silently.) It is normally
somewhat faster than Shellsort (perhaps 50% faster on vectors of
length a million and twice as fast at a billion) but has poor
performance in the rare worst case. (Peto's modification using a
pseudo-random midpoint is used to make the worst case rarer.) This is
not a stable sort, and ties may be reordered.

Method `"radix"`

relies on simple hashing to scale time linearly
with the input size, i.e., its asymptotic time complexity is O(n). The
specific variant and its implementation originated from the data.table
package and are due to Matt Dowle and Arun Srinivasan. For small
inputs (< 200), the implementation uses an insertion sort (O(n^2))
that operates in-place to avoid the allocation overhead of the radix
sort. For integer vectors of range less than 100,000, it switches to a
simpler and faster linear time counting sort. In all cases, the sort
is stable; the order of ties is preserved. It is the default method
for integer vectors and factors.

The `"radix"`

method generally outperforms the other methods,
especially for character vectors and small integers. Compared to quick
sort, it is slightly faster for vectors with large integer or real
values (but unlike quick sort, radix is stable and supports all
`na.last`

options). The implementation is orders of magnitude
faster than shell sort for character vectors, in part thanks to clever
use of the internal `CHARSXP`

table.

However, there are some caveats with the radix sort:

If `x`

is a `character`

vector, all elements must share
the same encoding. Only UTF-8 (including ASCII) and Latin-1
encodings are supported. Collation always follows the "C" locale.

Long vectors (with more than 2^32 elements) and `complex`

vectors are not supported yet.