<s>
sort	O
is	O
a	O
generic	O
function	O
in	O
the	O
C++	B-Language
Standard	I-Language
Library	I-Language
for	O
doing	O
comparison	B-Algorithm
sorting	I-Algorithm
.	O
</s>
<s>
The	O
function	O
originated	O
in	O
the	O
Standard	B-Application
Template	I-Application
Library	I-Application
(	O
STL	O
)	O
.	O
</s>
<s>
The	O
specific	O
sorting	B-Algorithm
algorithm	I-Algorithm
is	O
not	O
mandated	O
by	O
the	O
language	O
standard	O
and	O
may	O
vary	O
across	O
implementations	O
,	O
but	O
the	O
worst-case	B-General_Concept
asymptotic	O
complexity	O
of	O
the	O
function	O
is	O
specified	O
:	O
a	O
call	O
to	O
must	O
perform	O
no	O
more	O
than	O
comparisons	O
when	O
applied	O
to	O
a	O
range	O
of	O
elements	O
.	O
</s>
<s>
The	O
function	O
is	O
included	O
from	O
the	O
header	O
of	O
the	O
C++	B-Language
Standard	I-Language
Library	I-Language
,	O
and	O
carries	O
three	O
arguments	O
:	O
.	O
</s>
<s>
Here	O
,	O
is	O
a	O
templated	B-Application
type	O
that	O
must	O
be	O
a	O
random	B-General_Concept
access	I-General_Concept
iterator	O
,	O
and	O
and	O
must	O
define	O
a	O
sequence	O
of	O
values	O
,	O
i.e.	O
,	O
must	O
be	O
reachable	O
from	O
by	O
repeated	O
application	O
of	O
the	O
increment	O
operator	O
to	O
.	O
</s>
<s>
The	O
third	O
argument	O
,	O
also	O
of	O
a	O
templated	B-Application
type	O
,	O
denotes	O
a	O
comparison	O
predicate	O
.	O
</s>
<s>
The	O
same	O
functionality	O
using	O
a	O
container	B-Application
,	O
using	O
its	O
and	O
methods	O
to	O
obtain	O
iterators	O
:	O
</s>
<s>
is	O
specified	O
generically	O
,	O
so	O
that	O
it	O
can	O
work	O
on	O
any	O
random-access	B-General_Concept
container	B-Application
and	O
any	O
way	O
of	O
determining	O
that	O
an	O
element	O
of	O
such	O
a	O
container	B-Application
should	O
be	O
placed	O
before	O
another	O
element	O
.	O
</s>
<s>
Although	O
generically	O
specified	O
,	O
is	O
not	O
easily	O
applied	O
to	O
all	O
sorting	B-Algorithm
problems	I-Algorithm
.	O
</s>
<s>
Sort	O
while	O
maintaining	O
the	O
relation	O
with	O
,	O
i.e.	O
,	O
apply	O
the	O
same	O
permutation	B-Algorithm
to	O
that	O
sorts	O
.	O
</s>
<s>
Do	O
the	O
previous	O
without	O
copying	O
the	O
elements	O
of	O
and	O
into	O
a	O
new	O
array	O
of	O
pairs	O
,	O
sorting	B-Algorithm
,	O
and	O
moving	O
the	O
elements	O
back	O
into	O
the	O
original	O
arrays	O
(	O
which	O
would	O
require	O
temporary	O
space	O
)	O
.	O
</s>
<s>
In	O
previous	O
versions	O
of	O
C++	O
,	O
such	O
as	O
C++03	B-Language
,	O
only	O
average	O
complexity	O
was	O
required	O
to	O
be	O
.	O
</s>
<s>
This	O
was	O
to	O
allow	O
the	O
use	O
of	O
algorithms	O
like	O
(	O
median-of-3	O
)	O
quicksort	B-Algorithm
,	O
which	O
are	O
fast	O
in	O
the	O
average	B-General_Concept
case	I-General_Concept
,	O
indeed	O
significantly	O
faster	O
than	O
other	O
algorithms	O
like	O
heap	B-Application
sort	I-Application
with	O
optimal	O
worst-case	B-General_Concept
complexity	O
,	O
and	O
where	O
the	O
worst-case	B-General_Concept
quadratic	O
complexity	O
rarely	O
occurs	O
.	O
</s>
<s>
The	O
introduction	O
of	O
hybrid	B-Algorithm
algorithms	I-Algorithm
such	O
as	O
introsort	O
allowed	O
both	O
fast	O
average	B-General_Concept
performance	I-General_Concept
and	O
optimal	O
worst-case	B-General_Concept
performance	I-General_Concept
,	O
and	O
thus	O
the	O
complexity	O
requirements	O
were	O
tightened	O
in	O
later	O
standards	O
.	O
</s>
<s>
The	O
GNU	B-Application
Standard	I-Application
C++	I-Application
library	I-Application
,	O
for	O
example	O
,	O
uses	O
a	O
3-part	O
hybrid	O
sorting	B-Algorithm
algorithm	I-Algorithm
:	O
introsort	O
is	O
performed	O
first	O
(	O
introsort	O
itself	O
being	O
a	O
hybrid	O
of	O
quicksort	B-Algorithm
and	O
heap	B-Application
sort	I-Application
)	O
,	O
to	O
a	O
maximum	O
depth	O
given	O
by	O
2×log2	O
n	O
,	O
where	O
n	O
is	O
the	O
number	O
of	O
elements	O
,	O
followed	O
by	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
on	O
the	O
result	O
.	O
</s>
<s>
sort	O
is	O
not	O
stable	O
:	O
equivalent	O
elements	O
that	O
are	O
ordered	O
one	O
way	O
before	O
sorting	B-Algorithm
may	O
be	O
ordered	O
differently	O
after	O
sorting	B-Algorithm
.	O
</s>
<s>
This	O
allows	O
the	O
use	O
of	O
in-place	O
merge	O
sort	O
for	O
in-place	O
stable	O
sorting	B-Algorithm
and	O
regular	O
merge	O
sort	O
for	O
stable	O
sorting	B-Algorithm
with	O
additional	O
memory	O
.	O
</s>
<s>
Partial	B-Algorithm
sorting	I-Algorithm
is	O
implemented	O
by	O
,	O
which	O
takes	O
a	O
range	O
of	O
elements	O
and	O
an	O
integer	O
,	O
and	O
reorders	O
the	O
range	O
so	O
that	O
the	O
smallest	O
elements	O
are	O
in	O
the	O
first	O
positions	O
in	O
sorted	O
order	O
(	O
leaving	O
the	O
remaining	O
in	O
the	O
remaining	O
positions	O
,	O
in	O
some	O
unspecified	O
order	O
)	O
.	O
</s>
<s>
Historically	O
,	O
this	O
was	O
commonly	O
implemented	O
using	O
a	O
heap-based	O
algorithm	O
that	O
takes	O
worst-case	B-General_Concept
time	O
.	O
</s>
<s>
Selection	B-Algorithm
of	O
the	O
nth	O
element	O
is	O
implemented	O
by	O
nth_element	O
,	O
which	O
actually	O
implements	O
an	O
in-place	O
partial	B-Algorithm
sort	I-Algorithm
:	O
it	O
correctly	O
sorts	O
the	O
nth	O
element	O
,	O
and	O
also	O
ensures	O
that	O
this	O
element	O
partitions	O
so	O
elements	O
before	O
it	O
are	O
less	O
than	O
it	O
,	O
and	O
elements	O
after	O
it	O
are	O
greater	O
than	O
it	O
.	O
</s>
<s>
There	O
is	O
the	O
requirement	O
that	O
this	O
takes	O
linear	O
time	O
on	O
average	O
,	O
but	O
there	O
is	O
no	O
worst-case	B-General_Concept
requirement	O
;	O
these	O
requirements	O
are	O
exactly	O
met	O
by	O
quickselect	B-Algorithm
,	O
for	O
any	O
choice	O
of	O
pivot	O
strategy	O
.	O
</s>
<s>
Some	O
containers	B-Application
,	O
among	O
them	O
list	O
,	O
provide	O
specialised	O
version	O
of	O
sort	O
as	O
a	O
member	O
function	O
.	O
</s>
<s>
This	O
is	O
because	O
linked	O
lists	O
do	O
n't	O
have	O
random	B-General_Concept
access	I-General_Concept
(	O
and	O
therefore	O
ca	O
n't	O
use	O
the	O
regular	O
sort	O
function	O
)	O
;	O
and	O
the	O
specialised	O
version	O
also	O
preserves	O
the	O
values	O
list	O
iterators	O
point	O
to	O
.	O
</s>
<s>
Aside	O
from	O
,	O
the	O
C++	B-Language
standard	I-Language
library	I-Language
also	O
includes	O
the	O
function	O
from	O
the	B-Language
C	I-Language
standard	I-Language
library	I-Language
.	O
</s>
<s>
Compared	O
to	O
,	O
the	O
templated	B-Application
is	O
more	O
type-safe	O
since	O
it	O
does	O
not	O
require	O
access	O
to	O
data	O
items	O
through	O
unsafe	O
pointers	O
,	O
as	O
does	O
.	O
</s>
<s>
Also	O
,	O
accesses	O
the	O
comparison	O
function	O
using	O
a	O
function	O
pointer	O
,	O
necessitating	O
large	O
numbers	O
of	O
repeated	O
function	O
calls	O
,	O
whereas	O
in	O
,	O
comparison	O
functions	O
may	O
be	O
inlined	O
into	O
the	O
custom	O
object	O
code	O
generated	O
for	O
a	O
template	B-Application
instantiation	O
.	O
</s>
<s>
In	O
practice	O
,	O
C++	O
code	O
using	O
is	O
often	O
considerably	O
faster	O
at	O
sorting	B-Algorithm
simple	O
data	O
like	O
integers	O
than	O
equivalent	O
C	O
code	O
using	O
.	O
</s>
