<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
a	O
selection	B-Algorithm
algorithm	I-Algorithm
is	O
an	O
algorithm	O
for	O
finding	O
the	O
th	O
smallest	O
value	O
in	O
a	O
collection	O
of	O
ordered	O
values	O
,	O
such	O
as	O
numbers	O
.	O
</s>
<s>
The	O
value	O
that	O
it	O
finds	O
is	O
called	O
the	O
order	B-General_Concept
statistic	I-General_Concept
.	O
</s>
<s>
Selection	B-Algorithm
algorithms	I-Algorithm
include	O
quickselect	B-Algorithm
,	O
and	O
the	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
An	O
algorithm	O
for	O
the	O
selection	B-Algorithm
problem	I-Algorithm
takes	O
as	O
input	O
a	O
collection	O
of	O
values	O
,	O
and	O
a	O
It	O
outputs	O
the	O
smallest	O
of	O
these	O
values	O
,	O
or	O
,	O
in	O
some	O
versions	O
of	O
the	O
problem	O
,	O
a	O
collection	O
of	O
the	O
smallest	O
values	O
.	O
</s>
<s>
For	O
this	O
should	O
be	O
well-defined	O
,	O
it	O
should	O
be	O
possible	O
to	O
sort	B-Algorithm
the	O
values	O
into	O
an	O
order	O
from	O
smallest	O
to	O
largest	O
;	O
for	O
instance	O
,	O
they	O
may	O
be	O
numbers	O
,	O
or	O
some	O
other	O
kind	O
of	O
object	O
with	O
a	O
numeric	O
key	O
.	O
</s>
<s>
Often	O
,	O
selection	B-Algorithm
algorithms	I-Algorithm
are	O
restricted	O
to	O
a	O
comparison-based	O
model	O
of	O
computation	O
,	O
as	O
in	O
comparison	B-Algorithm
sort	I-Algorithm
algorithms	O
,	O
where	O
the	O
algorithm	O
has	O
access	O
to	O
a	O
comparison	O
operation	O
that	O
can	O
determine	O
the	O
relative	O
ordering	O
of	O
any	O
two	O
values	O
,	O
but	O
may	O
not	O
perform	O
any	O
other	O
kind	O
of	O
arithmetic	O
operations	O
on	O
these	O
values	O
.	O
</s>
<s>
If	O
the	O
output	O
of	O
the	O
sorting	B-Algorithm
algorithm	O
is	O
an	O
array	B-Data_Structure
,	O
jump	O
to	O
its	O
element	O
;	O
otherwise	O
,	O
scan	O
the	O
sorted	O
sequence	O
to	O
find	O
the	O
element	O
.	O
</s>
<s>
The	O
time	O
for	O
this	O
method	O
is	O
dominated	O
by	O
the	O
sorting	B-Algorithm
step	O
,	O
which	O
requires	O
time	O
using	O
a	O
Even	O
when	O
integer	B-Algorithm
sorting	I-Algorithm
algorithms	O
may	O
be	O
used	O
,	O
these	O
are	O
generally	O
slower	O
than	O
the	O
linear	O
time	O
that	O
may	O
be	O
achieved	O
using	O
specialized	O
selection	B-Algorithm
algorithms	I-Algorithm
.	O
</s>
<s>
Nevertheless	O
,	O
the	O
simplicity	O
of	O
this	O
approach	O
makes	O
it	O
attractive	O
,	O
especially	O
when	O
a	O
highly-optimized	O
sorting	B-Algorithm
routine	O
is	O
provided	O
as	O
part	O
of	O
a	O
runtime	O
library	O
,	O
but	O
a	O
selection	B-Algorithm
algorithm	I-Algorithm
is	O
not	O
.	O
</s>
<s>
For	O
a	O
sorting	B-Algorithm
algorithm	O
that	O
generates	O
one	O
item	O
at	O
a	O
time	O
,	O
such	O
as	O
selection	B-Algorithm
sort	I-Algorithm
,	O
the	O
scan	O
can	O
be	O
done	O
in	O
tandem	O
with	O
the	O
sort	B-Algorithm
,	O
and	O
the	O
sort	B-Algorithm
can	O
be	O
terminated	O
once	O
the	O
element	O
has	O
been	O
found	O
.	O
</s>
<s>
One	O
possible	O
design	O
of	O
a	O
consolation	O
bracket	O
in	O
a	O
single-elimination	O
tournament	O
,	O
in	O
which	O
the	O
teams	O
who	O
lost	O
to	O
the	O
eventual	O
winner	O
play	O
another	O
mini-tournament	O
to	O
determine	O
second	O
place	O
,	O
can	O
be	O
seen	O
as	O
an	O
instance	O
of	O
this	O
Applying	O
this	O
optimization	O
to	O
heapsort	B-Application
produces	O
the	O
heapselect	O
algorithm	O
,	O
which	O
can	O
select	O
the	O
smallest	O
value	O
in	O
This	O
is	O
fast	O
when	O
is	O
small	O
relative	O
but	O
degenerates	O
to	O
for	O
larger	O
values	O
such	O
as	O
the	O
choice	O
used	O
for	O
median	O
finding	O
.	O
</s>
<s>
In	O
particular	O
,	O
the	O
smallest	O
value	O
is	O
and	O
can	O
be	O
found	O
recursively	O
by	O
applying	O
the	O
same	O
selection	B-Algorithm
algorithm	I-Algorithm
then	O
the	O
smallest	O
value	O
is	O
the	O
pivot	O
,	O
and	O
it	O
can	O
be	O
returned	O
immediately	O
.	O
</s>
<s>
As	O
with	O
the	O
related	O
pivoting-based	O
quicksort	B-Algorithm
algorithm	O
,	O
the	O
partition	O
of	O
the	O
input	O
into	O
and	O
may	O
be	O
done	O
by	O
making	O
new	O
collections	O
for	O
these	O
sets	O
,	O
or	O
by	O
a	O
method	O
that	O
partitions	O
a	O
given	O
list	O
or	O
array	B-Data_Structure
data	O
type	O
in-place	O
.	O
</s>
<s>
If	O
the	O
pivot	O
were	O
exactly	O
at	O
the	O
median	O
of	O
the	O
input	O
,	O
then	O
each	O
recursive	O
call	O
would	O
have	O
at	O
most	O
half	O
as	O
many	O
values	O
as	O
the	O
previous	O
call	O
,	O
and	O
the	O
total	O
times	O
would	O
add	O
in	O
a	O
geometric	O
series	O
However	O
,	O
finding	O
the	O
median	O
is	O
itself	O
a	O
selection	B-Algorithm
problem	I-Algorithm
,	O
on	O
the	O
entire	O
original	O
input	O
.	O
</s>
<s>
Quickselect	B-Algorithm
chooses	O
the	O
pivot	O
uniformly	O
at	O
random	O
from	O
the	O
input	O
values	O
.	O
</s>
<s>
It	O
can	O
be	O
described	O
as	O
a	O
prune	B-Algorithm
and	I-Algorithm
search	I-Algorithm
algorithm	O
,	O
a	O
variant	O
of	O
quicksort	B-Algorithm
,	O
with	O
the	O
same	O
pivoting	O
strategy	O
,	O
but	O
where	O
quicksort	B-Algorithm
makes	O
two	O
recursive	O
calls	O
to	O
sort	B-Algorithm
the	O
two	O
subcollections	O
quickselect	B-Algorithm
only	O
makes	O
one	O
of	O
these	O
two	O
calls	O
.	O
</s>
<s>
The	O
Floyd	B-Algorithm
–	I-Algorithm
Rivest	I-Algorithm
algorithm	I-Algorithm
,	O
a	O
variation	O
of	O
quickselect	B-Algorithm
,	O
chooses	O
a	O
pivot	O
by	O
randomly	O
sampling	O
a	O
subset	O
of	O
data	O
values	O
,	O
for	O
some	O
sample	O
and	O
then	O
recursively	O
selecting	O
two	O
elements	O
somewhat	O
above	O
and	O
below	O
position	O
of	O
the	O
sample	O
to	O
use	O
as	O
pivots	O
.	O
</s>
<s>
This	O
method	O
can	O
achieve	O
an	O
expected	O
number	O
of	O
comparisons	O
that	O
is	O
In	O
their	O
original	O
work	O
,	O
Floyd	O
and	O
Rivest	O
claimed	O
that	O
the	O
term	O
could	O
be	O
made	O
as	O
small	O
as	O
by	O
a	O
recursive	O
sampling	O
scheme	O
,	O
but	O
the	O
correctness	O
of	O
their	O
analysis	O
has	O
been	O
Instead	O
,	O
more	O
rigorous	O
analysis	O
has	O
shown	O
that	O
a	O
version	O
of	O
their	O
algorithm	O
achieves	O
for	O
this	O
Although	O
the	O
usual	O
analysis	O
of	O
both	O
quickselect	B-Algorithm
and	O
the	O
Floyd	B-Algorithm
–	I-Algorithm
Rivest	I-Algorithm
algorithm	I-Algorithm
assumes	O
the	O
use	O
of	O
a	O
true	O
random	O
number	O
generator	O
,	O
a	O
version	O
of	O
the	O
Floyd	B-Algorithm
–	I-Algorithm
Rivest	I-Algorithm
algorithm	I-Algorithm
using	O
a	O
pseudorandom	B-Algorithm
number	I-Algorithm
generator	I-Algorithm
seeded	O
with	O
only	O
logarithmically	O
many	O
true	O
random	O
bits	O
has	O
been	O
proven	O
to	O
run	O
in	O
linear	O
time	O
with	O
high	O
probability	O
.	O
</s>
<s>
The	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
method	O
partitions	O
the	O
input	O
into	O
sets	O
of	O
five	O
elements	O
,	O
and	O
uses	O
some	O
other	O
non-recursive	O
method	O
to	O
find	O
the	O
median	O
of	O
each	O
of	O
these	O
sets	O
in	O
constant	O
time	O
per	O
set	O
.	O
</s>
<s>
Using	O
the	O
resulting	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
as	O
the	O
pivot	O
produces	O
a	O
partition	O
with	O
Thus	O
,	O
a	O
problem	O
on	O
elements	O
is	O
reduced	O
to	O
two	O
recursive	O
problems	O
on	O
elements	O
(	O
to	O
find	O
the	O
pivot	O
)	O
and	O
at	O
most	O
elements	O
(	O
after	O
the	O
pivot	O
is	O
used	O
)	O
.	O
</s>
<s>
The	O
deterministic	O
selection	B-Algorithm
algorithms	I-Algorithm
with	O
the	O
smallest	O
known	O
numbers	O
of	O
comparisons	O
,	O
for	O
values	O
of	O
that	O
are	O
far	O
from	O
are	O
based	O
on	O
the	O
concept	O
of	O
factories	O
,	O
introduced	O
in	O
1976	O
by	O
Arnold	O
Schönhage	O
,	O
Mike	O
Paterson	O
,	O
and	O
These	O
are	O
methods	O
that	O
build	O
partial	O
orders	O
of	O
certain	O
specified	O
types	O
,	O
on	O
small	O
subsets	O
of	O
input	O
values	O
,	O
by	O
using	O
comparisons	O
to	O
combine	O
smaller	O
partial	O
orders	O
.	O
</s>
<s>
When	O
data	O
is	O
already	O
organized	O
into	O
a	O
data	B-General_Concept
structure	I-General_Concept
,	O
it	O
may	O
be	O
possible	O
to	O
perform	O
selection	O
in	O
an	O
amount	O
of	O
time	O
that	O
is	O
sublinear	O
in	O
the	O
number	O
of	O
values	O
.	O
</s>
<s>
Selection	O
from	O
data	O
in	O
a	O
binary	B-Application
heap	I-Application
takes	O
This	O
is	O
independent	O
of	O
the	O
size	O
of	O
the	O
heap	O
,	O
and	O
faster	O
than	O
the	O
time	O
bound	O
that	O
would	O
be	O
obtained	O
from	O
This	O
same	O
method	O
can	O
be	O
applied	O
more	O
generally	O
to	O
data	O
organized	O
as	O
any	O
kind	O
of	O
heap-ordered	O
tree	O
(	O
a	O
tree	O
in	O
which	O
each	O
node	O
stores	O
one	O
value	O
in	O
which	O
the	O
parent	O
of	O
each	O
non-root	O
node	O
has	O
a	O
smaller	O
value	O
than	O
its	O
child	O
)	O
.	O
</s>
<s>
For	O
a	O
collection	O
of	O
data	O
values	O
undergoing	O
dynamic	O
insertions	O
and	O
deletions	O
,	O
the	O
order	B-Algorithm
statistic	I-Algorithm
tree	I-Algorithm
augments	O
a	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
structure	O
with	O
a	O
constant	O
amount	O
of	O
additional	O
information	O
per	O
tree	O
node	O
,	O
allowing	O
insertions	O
,	O
deletions	O
,	O
and	O
selection	O
queries	O
that	O
ask	O
for	O
the	O
element	O
in	O
the	O
current	O
set	O
to	O
all	O
be	O
performed	O
in	O
time	O
per	O
Going	O
beyond	O
the	O
comparison	O
model	O
of	O
computation	O
,	O
faster	O
times	O
per	O
operation	O
are	O
possible	O
for	O
values	O
that	O
are	O
small	O
integers	O
,	O
on	O
which	O
binary	O
arithmetic	O
operations	O
are	O
It	O
is	O
not	O
possible	O
for	O
a	O
streaming	O
algorithm	O
with	O
memory	O
sublinear	O
in	O
both	O
and	O
to	O
solve	O
selection	O
queries	O
exactly	O
for	O
dynamic	O
data	O
,	O
but	O
the	O
count	B-Algorithm
–	I-Algorithm
min	I-Algorithm
sketch	I-Algorithm
can	O
be	O
used	O
to	O
solve	O
selection	O
queries	O
approximately	O
,	O
by	O
finding	O
a	O
value	O
whose	O
position	O
in	O
the	O
ordering	O
of	O
the	O
elements	O
(	O
if	O
it	O
were	O
added	O
to	O
them	O
)	O
would	O
be	O
within	O
steps	O
of	O
,	O
for	O
a	O
sketch	O
whose	O
size	O
is	O
within	O
logarithmic	O
factors	O
of	O
.	O
</s>
<s>
The	O
running	O
time	O
of	O
the	O
selection	B-Algorithm
algorithms	I-Algorithm
described	O
above	O
is	O
necessary	O
,	O
because	O
a	O
selection	B-Algorithm
algorithm	I-Algorithm
that	O
can	O
handle	O
inputs	O
in	O
an	O
arbitrary	O
order	O
must	O
take	O
that	O
much	O
time	O
to	O
look	O
at	O
all	O
of	O
its	O
inputs	O
.	O
</s>
<s>
An	O
adversary	B-Algorithm
argument	I-Algorithm
,	O
in	O
which	O
the	O
outcome	O
of	O
each	O
comparison	O
is	O
chosen	O
in	O
order	O
to	O
maximize	O
(	O
subject	O
to	O
consistency	O
with	O
at	O
least	O
one	O
possible	O
ordering	O
)	O
rather	O
than	O
by	O
the	O
numerical	O
values	O
of	O
the	O
given	O
items	O
,	O
shows	O
that	O
it	O
is	O
possible	O
to	O
force	O
to	O
be	O
Therefore	O
,	O
the	O
worst-case	O
number	O
of	O
comparisons	O
needed	O
to	O
select	O
the	O
second	O
smallest	O
the	O
same	O
number	O
that	O
would	O
be	O
obtained	O
by	O
holding	O
a	O
single-elimination	O
tournament	O
with	O
a	O
run-off	O
tournament	O
among	O
the	O
values	O
that	O
lost	O
to	O
the	O
smallest	O
value	O
.	O
</s>
<s>
More	O
generally	O
,	O
selecting	O
the	O
element	O
out	O
of	O
requires	O
at	O
least	O
comparisons	O
,	O
in	O
the	O
average	O
case	O
,	O
matching	O
the	O
number	O
of	O
comparisons	O
of	O
the	O
Floyd	B-Algorithm
–	I-Algorithm
Rivest	I-Algorithm
algorithm	I-Algorithm
up	O
to	O
its	O
term	O
.	O
</s>
<s>
Knuth	O
supplies	O
the	O
following	O
triangle	O
of	O
numbers	O
summarizing	O
pairs	O
of	O
and	O
for	O
which	O
the	O
exact	O
number	O
of	O
comparisons	O
needed	O
by	O
an	O
optimal	O
selection	B-Algorithm
algorithm	I-Algorithm
is	O
known	O
.	O
</s>
<s>
A	O
notable	O
exception	O
is	O
the	O
Standard	B-Application
Template	I-Application
Library	I-Application
for	O
C++	B-Language
,	O
which	O
provides	O
a	O
templated	O
nth_element	O
method	O
with	O
a	O
guarantee	O
of	O
expected	O
linear	O
time	O
.	O
</s>
<s>
Python	B-Language
's	O
standard	O
library	O
(	O
since	O
2.4	O
)	O
includes	O
heapq.nsmallest	O
and	O
heapq.nlargest	O
subroutines	O
for	O
returning	O
the	O
smallest	O
or	O
largest	O
elements	O
from	O
a	O
collection	O
,	O
in	O
sorted	O
order	O
.	O
</s>
<s>
As	O
of	O
Python	B-Language
version	O
3.11	O
,	O
these	O
subroutines	O
use	O
heapselect	O
,	O
taking	O
time	O
to	O
return	O
a	O
list	O
of	O
elements	O
.	O
</s>
<s>
When	O
is	O
large	O
relative	O
they	O
revert	O
to	O
sorting	B-Algorithm
the	O
whole	O
input	O
and	O
then	O
returning	O
a	O
slice	O
of	O
the	O
resulting	O
sorted	O
array	B-Data_Structure
.	O
</s>
<s>
Since	O
2017	O
,	O
Matlab	B-Language
has	O
included	O
maxk( )	O
and	O
mink( )	O
functions	O
,	O
which	O
return	O
the	O
maximal	O
(	O
minimal	O
)	O
values	O
in	O
a	O
vector	O
as	O
well	O
as	O
their	O
indices	O
.	O
</s>
<s>
Quickselect	B-Algorithm
was	O
presented	O
without	O
analysis	O
by	O
Tony	O
Hoare	O
and	O
first	O
analyzed	O
in	O
a	O
1971	O
technical	O
report	O
by	O
The	O
first	O
known	O
linear	O
time	O
deterministic	O
selection	B-Algorithm
algorithm	I-Algorithm
is	O
the	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
method	O
,	O
published	O
in	O
1973	O
by	O
Manuel	O
Blum	O
,	O
Robert	O
W	O
.	O
Floyd	O
,	O
Vaughan	O
Pratt	O
,	O
Ron	O
Rivest	O
,	O
and	O
Robert	O
Tarjan	O
.	O
</s>
<s>
They	O
trace	O
the	O
formulation	O
of	O
the	O
selection	B-Algorithm
problem	I-Algorithm
to	O
work	O
of	O
Lewis	O
Carroll	O
in	O
1883	O
,	O
who	O
pointed	O
out	O
that	O
the	O
usual	O
design	O
of	O
single-elimination	O
sports	O
tournaments	O
does	O
not	O
guarantee	O
that	O
the	O
second-best	O
player	O
wins	O
second	O
place	O
,	O
and	O
to	O
work	O
of	O
Hugo	O
Steinhaus	O
circa	O
1930	O
,	O
who	O
followed	O
up	O
this	O
same	O
line	O
of	O
thought	O
by	O
asking	O
for	O
a	O
tournament	O
design	O
that	O
can	O
make	O
this	O
guarantee	O
,	O
with	O
a	O
minimum	O
number	O
of	O
games	O
played	O
(	O
that	O
is	O
,	O
</s>
