<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
the	O
median	O
of	O
medians	O
is	O
an	O
approximate	O
median	O
selection	B-Algorithm
algorithm	I-Algorithm
,	O
frequently	O
used	O
to	O
supply	O
a	O
good	O
pivot	O
for	O
an	O
exact	O
selection	B-Algorithm
algorithm	I-Algorithm
,	O
most	O
commonly	O
quickselect	B-Algorithm
,	O
that	O
selects	O
the	O
kth	O
smallest	O
element	O
of	O
an	O
initially	O
unsorted	O
array	B-Data_Structure
.	O
</s>
<s>
Using	O
this	O
approximate	O
median	O
as	O
an	O
improved	O
pivot	O
,	O
the	O
worst-case	O
complexity	O
of	O
quickselect	B-Algorithm
reduces	O
from	O
quadratic	O
to	O
linear	O
,	O
which	O
is	O
also	O
the	O
asymptotically	O
optimal	O
worst-case	O
complexity	O
of	O
any	O
selection	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
In	O
other	O
words	O
,	O
the	O
median	O
of	O
medians	O
is	O
an	O
approximate	O
median-selection	O
algorithm	O
that	O
helps	O
building	O
an	O
asymptotically	O
optimal	O
,	O
exact	O
general	O
selection	B-Algorithm
algorithm	I-Algorithm
(	O
especially	O
in	O
the	O
sense	O
of	O
worst-case	O
complexity	O
)	O
,	O
by	O
producing	O
good	O
pivot	O
elements	O
.	O
</s>
<s>
Median	O
of	O
medians	O
can	O
also	O
be	O
used	O
as	O
a	O
pivot	O
strategy	O
in	O
quicksort	B-Algorithm
,	O
yielding	O
an	O
optimal	O
algorithm	O
,	O
with	O
worst-case	O
complexity	O
.	O
</s>
<s>
Similarly	O
,	O
Median	O
of	O
medians	O
is	O
used	O
in	O
the	O
hybrid	O
introselect	B-Algorithm
algorithm	O
as	O
a	O
fallback	O
for	O
pivot	O
selection	O
at	O
each	O
iteration	O
until	O
kth	O
smallest	O
is	O
found	O
.	O
</s>
<s>
This	O
again	O
ensures	O
a	O
worst-case	O
linear	O
performance	O
,	O
in	O
addition	O
to	O
average-case	O
linear	O
performance	O
:	O
introselect	B-Algorithm
starts	O
with	O
quickselect	B-Algorithm
(	O
with	O
random	O
pivot	O
,	O
default	O
)	O
,	O
to	O
obtain	O
good	O
average	O
performance	O
,	O
and	O
then	O
falls	O
back	O
to	O
modified	O
quickselect	B-Algorithm
with	O
pivot	O
obtained	O
from	O
median	O
of	O
medians	O
if	O
the	O
progress	O
is	O
too	O
slow	O
.	O
</s>
<s>
Even	O
though	O
asymptotically	O
similar	O
,	O
such	O
a	O
hybrid	O
algorithm	O
will	O
have	O
a	O
lower	O
complexity	O
than	O
a	O
straightforward	O
introselect	B-Algorithm
up	O
to	O
a	O
constant	O
factor	O
(	O
both	O
in	O
average-case	O
and	O
worst-case	O
)	O
,	O
at	O
any	O
finite	O
length	O
.	O
</s>
<s>
The	O
algorithm	O
was	O
published	O
in	O
,	O
and	O
thus	O
is	O
sometimes	O
called	O
BFPRT	B-Algorithm
after	O
the	O
last	O
names	O
of	O
the	O
authors	O
.	O
</s>
<s>
In	O
the	O
original	O
paper	O
the	O
algorithm	O
was	O
referred	O
to	O
as	O
PICK	O
,	O
referring	O
to	O
quickselect	B-Algorithm
as	O
"	O
FIND	O
"	O
.	O
</s>
<s>
Quickselect	B-Algorithm
is	O
linear-time	O
on	O
average	O
,	O
but	O
it	O
can	O
require	O
quadratic	O
time	O
with	O
poor	O
pivot	O
choices	O
.	O
</s>
<s>
This	O
is	O
because	O
quickselect	B-Algorithm
is	O
a	O
divide	B-Algorithm
and	I-Algorithm
conquer	I-Algorithm
algorithm	I-Algorithm
,	O
with	O
each	O
step	O
taking	O
time	O
in	O
the	O
size	O
of	O
the	O
remaining	O
search	O
set	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
worst-case	O
occurs	O
when	O
pivoting	O
on	O
the	O
smallest	O
element	O
at	O
each	O
step	O
,	O
such	O
as	O
applying	O
quickselect	B-Algorithm
for	O
the	O
maximum	O
element	O
to	O
already	O
sorted	O
data	O
and	O
taking	O
the	O
first	O
element	O
as	O
pivot	O
each	O
time	O
.	O
</s>
<s>
As	O
stated	O
before	O
,	O
median-of-medians	O
is	O
used	O
as	O
a	O
pivot	O
selection	O
strategy	O
in	O
the	O
quickselect	B-Algorithm
algorithm	O
,	O
which	O
in	O
pseudocode	B-Language
looks	O
as	O
follows	O
.	O
</s>
<s>
The	O
following	O
pseudocode	B-Language
assumes	O
that	O
left	O
,	O
right	O
,	O
and	O
the	O
list	O
use	O
one-based	O
numbering	O
and	O
that	O
select	O
is	O
initially	O
called	O
with	O
1	O
as	O
the	O
argument	O
to	O
left	O
and	O
the	O
length	O
of	O
the	O
list	O
as	O
the	O
argument	O
to	O
right	O
.	O
</s>
<s>
Note	O
that	O
calls	O
;	O
this	O
is	O
an	O
instance	O
of	O
mutual	B-Algorithm
recursion	I-Algorithm
.	O
</s>
<s>
Here	O
is	O
pseudocode	B-Language
that	O
performs	O
a	O
partition	O
about	O
the	O
element	O
list[pivotIndex]	O
:	O
</s>
<s>
The	O
subroutine	O
selects	O
the	O
median	O
of	O
a	O
group	O
of	O
at	O
most	O
five	O
elements	O
;	O
an	O
easy	O
way	O
to	O
implement	O
this	O
is	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
as	O
shown	O
below	O
.	O
</s>
<s>
It	O
can	O
also	O
be	O
implemented	O
as	O
a	O
decision	B-Algorithm
tree	I-Algorithm
.	O
</s>
<s>
Let	O
be	O
the	O
time	O
it	O
takes	O
to	O
run	O
a	O
median-of-medians	O
Quickselect	B-Algorithm
algorithm	O
on	O
an	O
array	B-Data_Structure
of	O
size	O
.	O
</s>
<s>
the	O
term	O
is	O
for	O
the	O
partitioning	O
work	O
to	O
create	O
the	O
two	O
sides	O
,	O
one	O
of	O
which	O
our	O
Quickselect	B-Algorithm
will	O
recurse	O
(	O
we	O
visited	O
each	O
element	O
a	O
constant	O
number	O
of	O
times	O
in	O
order	O
to	O
form	O
them	O
into	O
n/5	O
groups	O
and	O
take	O
each	O
median	O
in	O
time	O
)	O
.	O
</s>
