<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
partial	B-Algorithm
sorting	I-Algorithm
is	O
a	O
relaxed	O
variant	O
of	O
the	O
sorting	B-Algorithm
problem	I-Algorithm
.	O
</s>
<s>
Total	O
sorting	B-Algorithm
is	O
the	O
problem	O
of	O
returning	O
a	O
list	O
of	O
items	O
such	O
that	O
its	O
elements	O
all	O
appear	O
in	O
order	O
,	O
while	O
partial	B-Algorithm
sorting	I-Algorithm
is	O
returning	O
a	O
list	O
of	O
the	O
k	O
smallest	O
(	O
or	O
k	O
largest	O
)	O
elements	O
in	O
order	O
.	O
</s>
<s>
The	O
other	O
elements	O
(	O
above	O
the	O
k	O
smallest	O
ones	O
)	O
may	O
also	O
be	O
sorted	O
,	O
as	O
in	O
an	O
in-place	O
partial	B-Algorithm
sort	I-Algorithm
,	O
or	O
may	O
be	O
discarded	O
,	O
which	O
is	O
common	O
in	O
streaming	O
partial	B-Algorithm
sorts	I-Algorithm
.	O
</s>
<s>
A	O
common	O
practical	O
example	O
of	O
partial	B-Algorithm
sorting	I-Algorithm
is	O
computing	O
the	O
"	O
Top	O
100	O
"	O
of	O
some	O
list	O
.	O
</s>
<s>
In	O
terms	O
of	O
indices	O
,	O
in	O
a	O
partially	O
sorted	B-Algorithm
list	I-Algorithm
,	O
for	O
every	O
index	O
i	O
from	O
1	O
to	O
k	O
,	O
the	O
i-th	O
element	O
is	O
in	O
the	O
same	O
place	O
as	O
it	O
would	O
be	O
in	O
the	O
fully	O
sorted	B-Algorithm
list	I-Algorithm
:	O
element	O
i	O
of	O
the	O
partially	O
sorted	B-Algorithm
list	I-Algorithm
contains	O
order	B-General_Concept
statistic	I-General_Concept
i	O
of	O
the	O
input	O
list	O
.	O
</s>
<s>
Heaps	B-Application
admit	O
a	O
simple	O
single-pass	O
partial	B-Algorithm
sort	I-Algorithm
when	O
is	O
fixed	O
:	O
insert	O
the	O
first	O
elements	O
of	O
the	O
input	O
into	O
a	O
max-heap	B-Application
.	O
</s>
<s>
Then	O
make	O
one	O
pass	O
over	O
the	O
remaining	O
elements	O
,	O
add	O
each	O
to	O
the	O
heap	B-Application
in	O
turn	O
,	O
and	O
remove	O
the	O
largest	O
element	O
.	O
</s>
<s>
Each	O
insertion	O
operation	O
takes	O
time	O
,	O
resulting	O
in	O
time	O
overall	O
;	O
this	O
"	O
partial	O
heapsort	B-Application
"	O
algorithm	O
is	O
practical	O
for	O
small	O
values	O
of	O
and	O
in	O
online	B-Algorithm
settings	O
.	O
</s>
<s>
An	O
"	O
online	B-Algorithm
heapselect	O
"	O
algorithm	O
described	O
below	O
,	O
based	O
on	O
a	O
min-heap	B-Application
,	O
takes	O
.	O
</s>
<s>
A	O
further	O
relaxation	O
requiring	O
only	O
a	O
list	O
of	O
the	O
smallest	O
elements	O
,	O
but	O
without	O
requiring	O
that	O
these	O
be	O
ordered	O
,	O
makes	O
the	O
problem	O
equivalent	O
to	O
partition-based	O
selection	O
;	O
the	O
original	O
partial	B-Algorithm
sorting	I-Algorithm
problem	O
can	O
be	O
solved	O
by	O
such	O
a	O
selection	B-Algorithm
algorithm	I-Algorithm
to	O
obtain	O
an	O
array	O
where	O
the	O
first	O
elements	O
are	O
the	O
smallest	O
,	O
and	O
sorting	B-Algorithm
these	O
,	O
at	O
a	O
total	O
cost	O
of	O
operations	O
.	O
</s>
<s>
A	O
popular	O
choice	O
to	O
implement	O
this	O
algorithm	O
scheme	O
is	O
to	O
combine	O
quickselect	B-Algorithm
and	O
quicksort	B-Algorithm
;	O
the	O
result	O
is	O
sometimes	O
called	O
"	O
quickselsort	O
"	O
.	O
</s>
<s>
Common	O
in	O
current	O
(	O
as	O
of	O
2022	O
)	O
C++	B-Language
STL	O
implementations	O
is	O
a	O
pass	O
of	O
heapselect	O
for	O
a	O
list	O
of	O
k	O
elements	O
,	O
followed	O
by	O
a	O
heapsort	B-Application
for	O
the	O
final	O
result	O
.	O
</s>
<s>
More	O
efficient	O
than	O
the	O
aforementioned	O
are	O
specialized	O
partial	B-Algorithm
sorting	I-Algorithm
algorithms	O
based	O
on	O
mergesort	B-Algorithm
and	O
quicksort	B-Algorithm
.	O
</s>
<s>
In	O
the	O
quicksort	B-Algorithm
variant	O
,	O
there	O
is	O
no	O
need	O
to	O
recursively	O
sort	O
partitions	O
which	O
only	O
contain	O
elements	O
that	O
would	O
fall	O
after	O
the	O
'	O
th	O
place	O
in	O
the	O
final	O
sorted	O
array	O
(	O
starting	O
from	O
the	O
"	O
left	O
"	O
boundary	O
)	O
.	O
</s>
<s>
The	O
resulting	O
algorithm	O
is	O
called	O
partial	O
quicksort	B-Algorithm
and	O
requires	O
an	O
expected	O
time	O
of	O
only	O
,	O
and	O
is	O
quite	O
efficient	O
in	O
practice	O
,	O
especially	O
if	O
a	O
selection	B-Algorithm
sort	I-Algorithm
is	O
used	O
as	O
a	O
base	O
case	O
when	O
becomes	O
small	O
relative	O
to	O
.	O
</s>
<s>
Pivot	O
selection	O
along	O
the	O
lines	O
of	O
the	O
worst-case	O
linear	O
time	O
selection	B-Algorithm
algorithm	I-Algorithm
(	O
see	O
)	O
could	O
be	O
used	O
to	O
get	O
better	O
worst-case	O
performance	O
.	O
</s>
<s>
Partial	O
quicksort	B-Algorithm
,	O
quickselect	B-Algorithm
(	O
including	O
the	O
multiple	O
variant	O
)	O
,	O
and	O
quicksort	B-Algorithm
can	O
all	O
be	O
generalized	O
into	O
what	O
is	O
known	O
as	O
a	O
chunksort	O
.	O
</s>
<s>
Incremental	O
sorting	B-Algorithm
is	O
a	O
version	O
of	O
the	O
partial	B-Algorithm
sorting	I-Algorithm
problem	O
where	O
the	O
input	O
is	O
given	O
up	O
front	O
but	O
is	O
unknown	O
:	O
given	O
a	O
-sorted	O
array	O
,	O
it	O
should	O
be	O
possible	O
to	O
extend	O
the	O
partially	O
sorted	O
part	O
so	O
that	O
the	O
array	O
becomes	O
-sorted	O
.	O
</s>
<s>
Heaps	B-Application
lead	O
to	O
an	O
"	O
online	B-Algorithm
heapselect	O
"	O
solution	O
to	O
incremental	O
partial	B-Algorithm
sorting	I-Algorithm
:	O
first	O
"	O
heapify	B-Application
"	O
,	O
in	O
linear	O
time	O
,	O
the	O
complete	O
input	O
array	O
to	O
produce	O
a	O
min-heap	B-Application
.	O
</s>
<s>
Then	O
extract	O
the	O
minimum	O
of	O
the	O
heap	B-Application
times	O
.	O
</s>
<s>
A	O
different	O
incremental	O
sort	O
can	O
be	O
obtained	O
by	O
modifying	O
quickselect	B-Algorithm
.	O
</s>
<s>
The	O
version	O
due	O
to	O
Paredes	O
and	O
Navarro	O
maintains	O
a	O
stack	B-Application
of	O
pivots	O
across	O
calls	O
,	O
so	O
that	O
incremental	O
sorting	B-Algorithm
can	O
be	O
accomplished	O
by	O
repeatedly	O
requesting	O
the	O
smallest	O
item	O
of	O
an	O
array	O
from	O
the	O
following	O
algorithm	O
:	O
</s>
<s>
The	O
stack	B-Application
is	O
initialized	O
to	O
contain	O
only	O
the	O
length	O
of	O
.	O
</s>
<s>
-sorting	O
the	O
array	O
is	O
done	O
by	O
calling	O
for	O
;	O
this	O
sequence	O
of	O
calls	O
has	O
average-case	O
complexity	O
,	O
which	O
is	O
asymptotically	O
equivalent	O
to	O
.	O
</s>
<s>
The	O
worst-case	O
time	O
is	O
quadratic	O
,	O
but	O
this	O
can	O
be	O
fixed	O
by	O
replacing	O
the	O
random	O
pivot	O
selection	O
by	O
the	O
median	B-Algorithm
of	I-Algorithm
medians	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
C++	B-Language
standard	O
specifies	O
a	O
library	O
function	O
called	O
std::partial_sort	O
.	O
</s>
<s>
The	O
Python	B-Language
standard	O
library	O
includes	O
functions	O
nlargest	O
and	O
nsmallest	O
in	O
its	O
heapq	O
module	O
.	O
</s>
