<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
heapsort	B-Application
is	O
a	O
comparison-based	B-Algorithm
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Heapsort	B-Application
can	O
be	O
thought	O
of	O
as	O
an	O
improved	O
selection	B-Algorithm
sort	I-Algorithm
:	O
like	O
selection	B-Algorithm
sort	I-Algorithm
,	O
heapsort	B-Application
divides	O
its	O
input	O
into	O
a	O
sorted	O
and	O
an	O
unsorted	O
region	O
,	O
and	O
it	O
iteratively	O
shrinks	O
the	O
unsorted	O
region	O
by	O
extracting	O
the	O
largest	O
element	O
from	O
it	O
and	O
inserting	O
it	O
into	O
the	O
sorted	O
region	O
.	O
</s>
<s>
Unlike	O
selection	B-Algorithm
sort	I-Algorithm
,	O
heapsort	B-Application
does	O
not	O
waste	O
time	O
with	O
a	O
linear-time	O
scan	O
of	O
the	O
unsorted	O
region	O
;	O
rather	O
,	O
heap	B-Application
sort	I-Application
maintains	O
the	O
unsorted	O
region	O
in	O
a	O
heap	B-Application
data	I-Application
structure	I-Application
to	O
more	O
quickly	O
find	O
the	O
largest	O
element	O
in	O
each	O
step	O
.	O
</s>
<s>
Although	O
somewhat	O
slower	O
in	O
practice	O
on	O
most	O
machines	O
than	O
a	O
well-implemented	O
quicksort	B-Algorithm
,	O
it	O
has	O
the	O
advantage	O
of	O
a	O
more	O
favorable	O
worst-case	O
runtime	O
(	O
and	O
as	O
such	O
is	O
used	O
by	O
Introsort	O
as	O
a	O
fallback	O
should	O
it	O
detect	O
that	O
quicksort	B-Algorithm
is	O
becoming	O
degenerate	O
)	O
.	O
</s>
<s>
Heapsort	B-Application
is	O
an	O
in-place	B-Algorithm
algorithm	I-Algorithm
,	O
but	O
it	O
is	O
not	O
a	O
stable	O
sort	O
.	O
</s>
<s>
Heapsort	B-Application
was	O
invented	O
by	O
J	O
.	O
W	O
.	O
J	O
.	O
Williams	O
in	O
1964	O
.	O
</s>
<s>
This	O
was	O
also	O
the	O
birth	O
of	O
the	O
heap	B-Application
,	O
presented	O
already	O
by	O
Williams	O
as	O
a	O
useful	O
data	O
structure	O
in	O
its	O
own	O
right	O
.	O
</s>
<s>
In	O
the	O
same	O
year	O
,	O
Robert	O
W	O
.	O
Floyd	O
published	O
an	O
improved	O
version	O
that	O
could	O
sort	O
an	O
array	B-Data_Structure
in-place	B-Algorithm
,	O
continuing	O
his	O
earlier	O
research	O
into	O
the	O
treesort	B-Algorithm
algorithm	O
.	O
</s>
<s>
The	O
heapsort	B-Application
algorithm	O
can	O
be	O
divided	O
into	O
two	O
parts	O
.	O
</s>
<s>
In	O
the	O
first	O
step	O
,	O
a	O
heap	B-Application
is	O
built	O
out	O
of	O
the	O
data	O
(	O
see	O
)	O
.	O
</s>
<s>
The	O
heap	B-Application
is	O
often	O
placed	O
in	O
an	O
array	B-Data_Structure
with	O
the	O
layout	O
of	O
a	O
complete	O
binary	O
tree	O
.	O
</s>
<s>
The	O
complete	O
binary	O
tree	O
maps	O
the	O
binary	O
tree	O
structure	O
into	O
the	O
array	B-Data_Structure
indices	O
;	O
each	O
array	B-Data_Structure
index	O
represents	O
a	O
node	O
;	O
the	O
index	O
of	O
the	O
node	O
's	O
parent	O
,	O
left	O
child	O
branch	O
,	O
or	O
right	O
child	O
branch	O
are	O
simple	O
expressions	O
.	O
</s>
<s>
In	O
the	O
second	O
step	O
,	O
a	O
sorted	O
array	B-Data_Structure
is	O
created	O
by	O
repeatedly	O
removing	O
the	O
largest	O
element	O
from	O
the	O
heap	B-Application
(	O
the	O
root	O
of	O
the	O
heap	B-Application
)	O
,	O
and	O
inserting	O
it	O
into	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
The	O
heap	B-Application
is	O
updated	O
after	O
each	O
removal	O
to	O
maintain	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
Once	O
all	O
objects	O
have	O
been	O
removed	O
from	O
the	O
heap	B-Application
,	O
the	O
result	O
is	O
a	O
sorted	O
array	B-Data_Structure
.	O
</s>
<s>
Heapsort	B-Application
can	O
be	O
performed	O
in	O
place	O
.	O
</s>
<s>
The	O
array	B-Data_Structure
can	O
be	O
split	O
into	O
two	O
parts	O
,	O
the	O
sorted	O
array	B-Data_Structure
and	O
the	O
heap	B-Application
.	O
</s>
<s>
The	O
storage	O
of	O
heaps	B-Application
as	O
arrays	O
is	O
diagrammed	O
here	O
.	O
</s>
<s>
The	O
heap	B-Application
's	O
invariant	O
is	O
preserved	O
after	O
each	O
extraction	O
,	O
so	O
the	O
only	O
cost	O
is	O
that	O
of	O
extraction	O
.	O
</s>
<s>
The	O
heapsort	B-Application
algorithm	O
involves	O
preparing	O
the	O
list	O
by	O
first	O
turning	O
it	O
into	O
a	O
max	B-Application
heap	I-Application
.	O
</s>
<s>
The	O
algorithm	O
then	O
repeatedly	O
swaps	O
the	O
first	O
value	O
of	O
the	O
list	O
with	O
the	O
last	O
value	O
,	O
decreasing	O
the	O
range	O
of	O
values	O
considered	O
in	O
the	O
heap	B-Application
operation	O
by	O
one	O
,	O
and	O
sifting	O
the	O
new	O
first	O
value	O
into	O
its	O
position	O
in	O
the	O
heap	B-Application
.	O
</s>
<s>
Also	O
referred	O
to	O
as	O
,	O
this	O
builds	O
a	O
heap	B-Application
from	O
a	O
list	O
in	O
operations	O
.	O
</s>
<s>
Call	O
the	O
function	O
on	O
the	O
list	O
to	O
sift	O
the	O
new	O
first	O
element	O
to	O
its	O
appropriate	O
index	O
in	O
the	O
heap	B-Application
.	O
</s>
<s>
The	O
following	O
is	O
a	O
simple	O
way	O
to	O
implement	O
the	O
algorithm	O
in	O
pseudocode	B-Language
.	O
</s>
<s>
Arrays	O
are	O
zero-based	B-Data_Structure
and	O
swap	O
is	O
used	O
to	O
exchange	O
two	O
elements	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Note	O
that	O
during	O
the	O
sort	O
,	O
the	O
largest	O
element	O
is	O
at	O
the	O
root	O
of	O
the	O
heap	B-Application
at	O
a[0],	O
while	O
at	O
the	O
end	O
of	O
the	O
sort	O
,	O
the	O
largest	O
element	O
is	O
in	O
a[end]	O
.	O
</s>
<s>
The	O
sorting	B-Algorithm
routine	O
uses	O
two	O
subroutines	O
,	O
heapify	B-Application
and	O
siftDown	O
.	O
</s>
<s>
The	O
former	O
is	O
the	O
common	O
in-place	B-Algorithm
heap	B-Application
construction	O
routine	O
,	O
while	O
the	O
latter	O
is	O
a	O
common	O
subroutine	O
for	O
implementing	O
heapify	B-Application
.	O
</s>
<s>
The	O
heapify	B-Application
procedure	O
can	O
be	O
thought	O
of	O
as	O
building	O
a	O
heap	B-Application
from	O
the	O
bottom	O
up	O
by	O
successively	O
sifting	O
downward	O
to	O
establish	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
An	O
alternative	O
version	O
(	O
shown	O
below	O
)	O
that	O
builds	O
the	O
heap	B-Application
top-down	O
and	O
sifts	O
upward	O
may	O
be	O
simpler	O
to	O
understand	O
.	O
</s>
<s>
This	O
siftUp	O
version	O
can	O
be	O
visualized	O
as	O
starting	O
with	O
an	O
empty	O
heap	B-Application
and	O
successively	O
inserting	O
elements	O
,	O
whereas	O
the	O
siftDown	O
version	O
given	O
above	O
treats	O
the	O
entire	O
input	O
array	B-Data_Structure
as	O
a	O
full	O
but	O
"	O
broken	O
"	O
heap	B-Application
and	O
"	O
repairs	O
"	O
it	O
starting	O
from	O
the	O
last	O
non-trivial	O
sub-heap	O
(	O
that	O
is	O
,	O
the	O
last	O
parent	O
node	O
)	O
.	O
</s>
<s>
Also	O
,	O
the	O
siftDown	O
version	O
of	O
heapify	B-Application
has	O
time	O
complexity	O
,	O
while	O
the	O
siftUp	O
version	O
given	O
below	O
has	O
time	O
complexity	O
due	O
to	O
its	O
equivalence	O
with	O
inserting	O
each	O
element	O
,	O
one	O
at	O
a	O
time	O
,	O
into	O
an	O
empty	O
heap	B-Application
.	O
</s>
<s>
The	O
crux	O
is	O
that	O
there	O
are	O
many	O
(	O
exponentially	O
many	O
)	O
more	O
"	O
deep	O
"	O
nodes	O
than	O
there	O
are	O
"	O
shallow	O
"	O
nodes	O
in	O
a	O
heap	B-Application
,	O
so	O
that	O
siftUp	O
may	O
have	O
its	O
full	O
logarithmic	O
running-time	O
on	O
the	O
approximately	O
linear	O
number	O
of	O
calls	O
made	O
on	O
the	O
nodes	O
at	O
or	O
near	O
the	O
"	O
bottom	O
"	O
of	O
the	O
heap	B-Application
.	O
</s>
<s>
Thus	O
,	O
when	O
the	O
siftDown	O
heapify	B-Application
begins	O
and	O
is	O
calling	O
siftDown	O
on	O
the	O
bottom	O
and	O
most	O
numerous	O
node-layers	O
,	O
each	O
sifting	O
call	O
will	O
incur	O
,	O
at	O
most	O
,	O
a	O
number	O
of	O
swaps	O
equal	O
to	O
the	O
"	O
height	O
"	O
(	O
from	O
the	O
bottom	O
of	O
the	O
heap	B-Application
)	O
of	O
the	O
node	O
on	O
which	O
the	O
sifting	O
call	O
is	O
made	O
.	O
</s>
<s>
The	O
heapsort	B-Application
algorithm	O
itself	O
has	O
time	O
complexity	O
using	O
either	O
version	O
of	O
heapify	B-Application
.	O
</s>
<s>
input	O
:	O
start	O
represents	O
the	O
limit	O
of	O
how	O
far	O
up	O
the	O
heap	B-Application
to	O
sift	O
.	O
</s>
<s>
Note	O
that	O
unlike	O
siftDown	O
approach	O
where	O
,	O
after	O
each	O
swap	O
,	O
you	O
need	O
to	O
call	O
only	O
the	O
siftDown	O
subroutine	O
to	O
fix	O
the	O
broken	O
heap	B-Application
;	O
the	O
siftUp	O
subroutine	O
alone	O
cannot	O
fix	O
the	O
broken	O
heap	B-Application
.	O
</s>
<s>
The	O
heap	B-Application
needs	O
to	O
be	O
built	O
every	O
time	O
after	O
a	O
swap	O
by	O
calling	O
the	O
heapify	B-Application
procedure	O
since	O
"	O
siftUp	O
"	O
assumes	O
that	O
the	O
element	O
getting	O
swapped	O
ends	O
up	O
in	O
its	O
final	O
place	O
,	O
as	O
opposed	O
to	O
"	O
siftDown	O
"	O
allows	O
for	O
continuous	O
adjustments	O
of	O
items	O
lower	O
in	O
the	O
heap	B-Application
until	O
the	O
invariant	O
is	O
satisfied	O
.	O
</s>
<s>
The	O
adjusted	O
pseudocode	B-Language
for	O
using	O
siftUp	O
approach	O
is	O
given	O
below	O
.	O
</s>
<s>
The	O
most	O
important	O
variation	O
to	O
the	O
basic	O
algorithm	O
,	O
which	O
is	O
included	O
in	O
all	O
practical	O
implementations	O
,	O
is	O
a	O
heap-construction	O
algorithm	O
by	O
Floyd	O
which	O
runs	O
in	O
time	O
and	O
uses	O
siftdown	O
rather	O
than	O
siftup	O
,	O
avoiding	O
the	O
need	O
to	O
implement	O
siftup	O
at	O
all	O
.	O
</s>
<s>
Rather	O
than	O
starting	O
with	O
a	O
trivial	O
heap	B-Application
and	O
repeatedly	O
adding	O
leaves	O
,	O
Floyd	O
's	O
algorithm	O
starts	O
with	O
the	O
leaves	O
,	O
observing	O
that	O
they	O
are	O
trivial	O
but	O
valid	O
heaps	B-Application
by	O
themselves	O
,	O
and	O
then	O
adds	O
parents	O
.	O
</s>
<s>
Starting	O
with	O
element	O
and	O
working	O
backwards	O
,	O
each	O
internal	O
node	O
is	O
made	O
the	O
root	O
of	O
a	O
valid	O
heap	B-Application
by	O
sifting	O
down	O
.	O
</s>
<s>
The	O
last	O
step	O
is	O
sifting	O
down	O
the	O
first	O
element	O
,	O
after	O
which	O
the	O
entire	O
array	B-Data_Structure
obeys	O
the	O
heap	B-Application
property	I-Application
.	O
</s>
<s>
The	O
worst-case	O
number	O
of	O
comparisons	O
during	O
the	O
Floyd	O
's	O
heap-construction	O
phase	O
of	O
heapsort	B-Application
is	O
known	O
to	O
be	O
equal	O
to	O
,	O
where	O
is	O
the	O
number	O
of	O
1	O
bits	O
in	O
the	O
binary	O
representation	O
of	O
and	O
is	O
number	O
of	O
trailing	O
0	O
bits	O
.	O
</s>
<s>
The	O
standard	O
implementation	O
of	O
Floyd	O
's	O
heap-construction	O
algorithm	O
causes	O
a	O
large	O
number	O
of	O
cache	O
misses	O
once	O
the	O
size	O
of	O
the	O
data	O
exceeds	O
that	O
of	O
the	O
CPU	B-General_Concept
cache	I-General_Concept
.	O
</s>
<s>
Much	O
better	O
performance	O
on	O
large	O
data	O
sets	O
can	O
be	O
obtained	O
by	O
merging	O
in	O
depth-first	B-Algorithm
order	O
,	O
combining	O
subheaps	O
as	O
soon	O
as	O
possible	O
,	O
rather	O
than	O
combining	O
all	O
subheaps	O
on	O
one	O
level	O
before	O
proceeding	O
to	O
the	O
one	O
above	O
.	O
</s>
<s>
Bottom-up	O
heapsort	B-Application
is	O
a	O
variant	O
which	O
reduces	O
the	O
number	O
of	O
comparisons	O
required	O
by	O
a	O
significant	O
factor	O
.	O
</s>
<s>
While	O
ordinary	O
heapsort	B-Application
requires	O
comparisons	O
worst-case	O
and	O
on	O
average	O
,	O
the	O
bottom-up	O
variant	O
requires	O
comparisons	O
on	O
average	O
,	O
and	O
in	O
the	O
worst	O
case	O
.	O
</s>
<s>
integer	O
keys	O
)	O
then	O
the	O
difference	O
is	O
unimportant	O
,	O
as	O
top-down	O
heapsort	B-Application
compares	O
values	O
that	O
have	O
already	O
been	O
loaded	O
from	O
memory	O
.	O
</s>
<s>
If	O
,	O
however	O
,	O
comparisons	O
require	O
a	O
function	O
call	O
or	O
other	O
complex	O
logic	O
,	O
then	O
bottom-up	O
heapsort	B-Application
is	O
advantageous	O
.	O
</s>
<s>
The	O
change	O
improves	O
the	O
linear-time	O
heap-building	O
phase	O
somewhat	O
,	O
but	O
is	O
more	O
significant	O
in	O
the	O
second	O
phase	O
.	O
</s>
<s>
Like	O
ordinary	O
heapsort	B-Application
,	O
each	O
iteration	O
of	O
the	O
second	O
phase	O
extracts	O
the	O
top	O
of	O
the	O
heap	B-Application
,	O
,	O
and	O
fills	O
the	O
gap	O
it	O
leaves	O
with	O
,	O
then	O
sifts	O
this	O
latter	O
element	O
down	O
the	O
heap	B-Application
.	O
</s>
<s>
But	O
this	O
element	O
comes	O
from	O
the	O
lowest	O
level	O
of	O
the	O
heap	B-Application
,	O
meaning	O
it	O
is	O
one	O
of	O
the	O
smallest	O
elements	O
in	O
the	O
heap	B-Application
,	O
so	O
the	O
sift-down	O
will	O
likely	O
take	O
many	O
steps	O
to	O
move	O
it	O
back	O
down	O
.	O
</s>
<s>
In	O
ordinary	O
heapsort	B-Application
,	O
each	O
step	O
of	O
the	O
sift-down	O
requires	O
two	O
comparisons	O
,	O
to	O
find	O
the	O
minimum	O
of	O
three	O
elements	O
:	O
the	O
new	O
node	O
and	O
its	O
two	O
children	O
.	O
</s>
<s>
Bottom-up	O
heapsort	B-Application
instead	O
finds	O
the	O
path	O
of	O
largest	O
children	O
to	O
the	O
leaf	O
level	O
of	O
the	O
tree	O
(	O
as	O
if	O
it	O
were	O
inserting	O
−∞	O
)	O
using	O
only	O
one	O
comparison	O
per	O
level	O
.	O
</s>
<s>
This	O
is	O
the	O
same	O
location	O
as	O
ordinary	O
heapsort	B-Application
finds	O
,	O
and	O
requires	O
the	O
same	O
number	O
of	O
exchanges	O
to	O
perform	O
the	O
insert	O
,	O
but	O
fewer	O
comparisons	O
are	O
required	O
to	O
find	O
that	O
location	O
.	O
</s>
<s>
Because	O
it	O
goes	O
all	O
the	O
way	O
to	O
the	O
bottom	O
and	O
then	O
comes	O
back	O
up	O
,	O
it	O
is	O
called	O
heapsort	B-Application
with	O
bounce	O
by	O
some	O
authors	O
.	O
</s>
<s>
Bottom-up	O
heapsort	B-Application
was	O
announced	O
as	O
beating	O
quicksort	B-Algorithm
(	O
with	O
median-of-three	O
pivot	O
selection	O
)	O
on	O
arrays	O
of	O
size	O
≥16000	O
.	O
</s>
<s>
A	O
2008	O
re-evaluation	O
of	O
this	O
algorithm	O
showed	O
it	O
to	O
be	O
no	O
faster	O
than	O
ordinary	O
heapsort	B-Application
for	O
integer	O
keys	O
,	O
presumably	O
because	O
modern	O
branch	B-General_Concept
prediction	I-General_Concept
nullifies	O
the	O
cost	O
of	O
the	O
predictable	O
comparisons	O
which	O
bottom-up	O
heapsort	B-Application
manages	O
to	O
avoid	O
.	O
</s>
<s>
A	O
variant	O
which	O
uses	O
two	O
extra	O
bits	O
per	O
internal	O
node	O
(	O
n−1	O
bits	O
total	O
for	O
an	O
n-element	O
heap	B-Application
)	O
to	O
cache	O
information	O
about	O
which	O
child	O
is	O
greater	O
(	O
two	O
bits	O
are	O
required	O
to	O
store	O
three	O
cases	O
:	O
left	O
,	O
right	O
,	O
and	O
unknown	O
)	O
uses	O
less	O
than	O
compares	O
.	O
</s>
<s>
Ternary	O
heapsort	B-Application
uses	O
a	O
ternary	B-Application
heap	I-Application
instead	O
of	O
a	O
binary	B-Application
heap	I-Application
;	O
that	O
is	O
,	O
each	O
element	O
in	O
the	O
heap	B-Application
has	O
three	O
children	O
.	O
</s>
<s>
This	O
is	O
because	O
each	O
sift-down	O
step	O
in	O
a	O
ternary	B-Application
heap	I-Application
requires	O
three	O
comparisons	O
and	O
one	O
swap	O
,	O
whereas	O
in	O
a	O
binary	B-Application
heap	I-Application
two	O
comparisons	O
and	O
one	O
swap	O
are	O
required	O
.	O
</s>
<s>
Two	O
levels	O
in	O
a	O
ternary	B-Application
heap	I-Application
cover	O
32	O
=	O
9	O
elements	O
,	O
doing	O
more	O
work	O
with	O
the	O
same	O
number	O
of	O
comparisons	O
as	O
three	O
levels	O
in	O
the	O
binary	B-Application
heap	I-Application
,	O
which	O
only	O
cover	O
23	O
=	O
8	O
.	O
</s>
<s>
This	O
is	O
primarily	O
of	O
academic	O
interest	O
,	O
or	O
as	O
a	O
student	O
exercise	O
,	O
as	O
the	O
additional	O
complexity	O
is	O
not	O
worth	O
the	O
minor	O
savings	O
,	O
and	O
bottom-up	O
heapsort	B-Application
beats	O
both	O
.	O
</s>
<s>
Memory-optimized	O
heapsort	B-Application
improves	O
heapsort	B-Application
's	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
by	O
increasing	O
the	O
number	O
of	O
children	O
even	O
more	O
.	O
</s>
<s>
This	O
increases	O
the	O
number	O
of	O
comparisons	O
,	O
but	O
because	O
all	O
children	O
are	O
stored	O
consecutively	O
in	O
memory	O
,	O
reduces	O
the	O
number	O
of	O
cache	B-General_Concept
lines	I-General_Concept
accessed	O
during	O
heap	B-Application
traversal	O
,	O
a	O
net	O
performance	O
improvement	O
.	O
</s>
<s>
Out-of-place	O
heapsort	B-Application
improves	O
on	O
bottom-up	O
heapsort	B-Application
by	O
eliminating	O
the	O
worst	O
case	O
,	O
guaranteeing	O
comparisons	O
.	O
</s>
<s>
It	O
turns	O
out	O
that	O
this	O
can	O
be	O
used	O
as	O
a	O
primitive	O
in	O
an	O
in-place	B-Algorithm
(	O
and	O
non-recursive	O
)	O
"	O
QuickHeapsort	O
"	O
algorithm	O
.	O
</s>
<s>
First	O
,	O
you	O
perform	O
a	O
quicksort-like	O
partitioning	O
pass	O
,	O
but	O
reversing	O
the	O
order	O
of	O
the	O
partitioned	O
data	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Suppose	O
(	O
without	O
loss	O
of	O
generality	O
)	O
that	O
the	O
smaller	O
partition	O
is	O
the	O
one	O
greater	O
than	O
the	O
pivot	O
,	O
which	O
should	O
go	O
at	O
the	O
end	O
of	O
the	O
array	B-Data_Structure
,	O
but	O
our	O
reversed	O
partitioning	O
step	O
places	O
it	O
at	O
the	O
beginning	O
.	O
</s>
<s>
Form	O
a	O
heap	B-Application
out	O
of	O
the	O
smaller	O
partition	O
and	O
do	O
out-of-place	O
heapsort	B-Application
on	O
it	O
,	O
exchanging	O
the	O
extracted	O
maxima	O
with	O
values	O
from	O
the	O
end	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
These	O
are	O
less	O
than	O
the	O
pivot	O
,	O
meaning	O
less	O
than	O
any	O
value	O
in	O
the	O
heap	B-Application
,	O
so	O
serve	O
as	O
sentinel	O
values	O
.	O
</s>
<s>
Once	O
the	O
heapsort	B-Application
is	O
complete	O
(	O
and	O
the	O
pivot	O
moved	O
to	O
just	O
before	O
the	O
now-sorted	O
end	O
of	O
the	O
array	B-Data_Structure
)	O
,	O
the	O
order	O
of	O
the	O
partitions	O
has	O
been	O
reversed	O
,	O
and	O
the	O
larger	O
partition	O
at	O
the	O
beginning	O
of	O
the	O
array	B-Data_Structure
may	O
be	O
sorted	O
in	O
the	O
same	O
way	O
.	O
</s>
<s>
(	O
Because	O
there	O
is	O
no	O
non-tail	O
recursion	O
,	O
this	O
also	O
eliminates	O
quicksort	B-Algorithm
's	O
stack	O
usage	O
.	O
)	O
</s>
<s>
The	O
smoothsort	B-Application
algorithm	O
is	O
a	O
variation	O
of	O
heapsort	B-Application
developed	O
by	O
Edsger	O
W	O
.	O
Dijkstra	O
in	O
1981	O
.	O
</s>
<s>
Like	O
heapsort	B-Application
,	O
smoothsort	B-Application
's	O
upper	O
bound	O
is	O
.	O
</s>
<s>
The	O
advantage	O
of	O
smoothsort	B-Application
is	O
that	O
it	O
comes	O
closer	O
to	O
time	O
if	O
the	O
input	B-Algorithm
is	I-Algorithm
already	I-Algorithm
sorted	I-Algorithm
to	I-Algorithm
some	I-Algorithm
degree	I-Algorithm
,	O
whereas	O
heapsort	B-Application
averages	O
regardless	O
of	O
the	O
initial	O
sorted	O
state	O
.	O
</s>
<s>
Due	O
to	O
its	O
complexity	O
,	O
smoothsort	B-Application
is	O
rarely	O
used	O
.	O
</s>
<s>
Levcopoulos	O
and	O
Petersson	O
describe	O
a	O
variation	O
of	O
heapsort	B-Application
based	O
on	O
a	O
heap	B-Application
of	O
Cartesian	B-Algorithm
trees	I-Algorithm
.	O
</s>
<s>
First	O
,	O
a	O
Cartesian	B-Algorithm
tree	I-Algorithm
is	O
built	O
from	O
the	O
input	O
in	O
time	O
,	O
and	O
its	O
root	O
is	O
placed	O
in	O
a	O
1-element	O
binary	B-Application
heap	I-Application
.	O
</s>
<s>
Then	O
we	O
repeatedly	O
extract	O
the	O
minimum	O
from	O
the	O
binary	B-Application
heap	I-Application
,	O
output	O
the	O
tree	O
's	O
root	O
element	O
,	O
and	O
add	O
its	O
left	O
and	O
right	O
children	O
(	O
if	O
any	O
)	O
which	O
are	O
themselves	O
Cartesian	B-Algorithm
trees	I-Algorithm
,	O
to	O
the	O
binary	B-Application
heap	I-Application
.	O
</s>
<s>
As	O
they	O
show	O
,	O
if	O
the	O
input	O
is	O
already	O
nearly	O
sorted	O
,	O
the	O
Cartesian	B-Algorithm
trees	I-Algorithm
will	O
be	O
very	O
unbalanced	O
,	O
with	O
few	O
nodes	O
having	O
left	O
and	O
right	O
children	O
,	O
resulting	O
in	O
the	O
binary	B-Application
heap	I-Application
remaining	O
small	O
,	O
and	O
allowing	O
the	O
algorithm	O
to	O
sort	O
more	O
quickly	O
than	O
for	O
inputs	O
that	O
are	O
already	O
nearly	O
sorted	O
.	O
</s>
<s>
Several	O
variants	O
such	O
as	O
weak	O
heapsort	B-Application
require	O
comparisons	O
in	O
the	O
worst	O
case	O
,	O
close	O
to	O
the	O
theoretical	O
minimum	O
,	O
using	O
one	O
extra	O
bit	O
of	O
state	O
per	O
node	O
.	O
</s>
<s>
While	O
this	O
extra	O
bit	O
makes	O
the	O
algorithms	O
not	O
truly	O
in-place	B-Algorithm
,	O
if	O
space	O
for	O
it	O
can	O
be	O
found	O
inside	O
the	O
element	O
,	O
these	O
algorithms	O
are	O
simple	O
and	O
efficient	O
,	O
but	O
still	O
slower	O
than	O
binary	B-Application
heaps	I-Application
if	O
key	O
comparisons	O
are	O
cheap	O
enough	O
(	O
e.g.	O
</s>
<s>
Katajainen	O
's	O
"	O
ultimate	O
heapsort	B-Application
"	O
requires	O
no	O
extra	O
storage	O
,	O
performs	O
comparisons	O
,	O
and	O
a	O
similar	O
number	O
of	O
element	O
moves	O
.	O
</s>
<s>
Heapsort	B-Application
primarily	O
competes	O
with	O
quicksort	B-Algorithm
,	O
another	O
very	O
efficient	O
general	O
purpose	O
in-place	B-Algorithm
comparison-based	B-Algorithm
sort	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Heapsort	B-Application
's	O
primary	O
advantages	O
are	O
its	O
simple	O
,	O
non-recursive	O
code	O
,	O
minimal	O
auxiliary	O
storage	O
requirement	O
,	O
and	O
reliably	O
good	O
performance	O
:	O
its	O
best	O
and	O
worst	O
cases	O
are	O
within	O
a	O
small	O
constant	O
factor	O
of	O
each	O
other	O
,	O
and	O
of	O
the	O
theoretical	O
lower	O
bound	O
on	O
comparison	B-Algorithm
sorts	I-Algorithm
.	O
</s>
<s>
While	O
it	O
cannot	O
do	O
better	O
than	O
for	O
pre-sorted	O
inputs	O
,	O
it	O
does	O
not	O
suffer	O
from	O
quicksort	B-Algorithm
's	O
worst	O
case	O
,	O
either	O
.	O
</s>
<s>
(	O
The	O
latter	O
can	O
be	O
avoided	O
with	O
careful	O
implementation	O
,	O
but	O
that	O
makes	O
quicksort	B-Algorithm
far	O
more	O
complex	O
,	O
and	O
one	O
of	O
the	O
most	O
popular	O
solutions	O
,	O
introsort	O
,	O
uses	O
heapsort	B-Application
for	O
the	O
purpose	O
.	O
)	O
</s>
<s>
Its	O
primary	O
disadvantages	O
are	O
its	O
poor	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
and	O
its	O
inherently	O
serial	O
nature	O
;	O
the	O
accesses	O
to	O
the	O
implicit	O
tree	O
are	O
widely	O
scattered	O
and	O
mostly	O
random	O
,	O
and	O
there	O
is	O
no	O
straightforward	O
way	O
to	O
convert	O
it	O
to	O
a	O
parallel	B-Operating_System
algorithm	I-Operating_System
.	O
</s>
<s>
This	O
makes	O
it	O
popular	O
in	O
embedded	B-Architecture
systems	I-Architecture
,	O
real-time	B-General_Concept
computing	I-General_Concept
,	O
and	O
systems	O
concerned	O
with	O
maliciously	O
chosen	O
inputs	O
,	O
such	O
as	O
the	O
Linux	O
kernel	O
.	O
</s>
<s>
It	O
is	O
also	O
a	O
good	O
choice	O
for	O
any	O
application	O
which	O
does	O
not	O
expect	O
to	O
be	O
bottlenecked	O
on	O
sorting	B-Algorithm
.	O
</s>
<s>
A	O
well-implemented	O
quicksort	B-Algorithm
is	O
usually	O
2	O
–	O
3	O
times	O
faster	O
than	O
heapsort	B-Application
.	O
</s>
<s>
Although	O
quicksort	B-Algorithm
requires	O
fewer	O
comparisons	O
,	O
this	O
is	O
a	O
minor	O
factor	O
.	O
</s>
<s>
The	O
main	O
advantage	O
of	O
quicksort	B-Algorithm
is	O
its	O
much	O
better	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
:	O
partitioning	O
is	O
a	O
linear	O
scan	O
with	O
good	O
spatial	O
locality	O
,	O
and	O
the	O
recursive	O
subdivision	O
has	O
good	O
temporal	B-General_Concept
locality	I-General_Concept
.	O
</s>
<s>
With	O
additional	O
effort	O
,	O
quicksort	B-Algorithm
can	O
also	O
be	O
implemented	O
in	O
mostly	O
branch-free	O
code	O
,	O
and	O
multiple	O
CPUs	O
can	O
be	O
used	O
to	O
sort	O
subpartitions	O
in	O
parallel	O
.	O
</s>
<s>
Thus	O
,	O
quicksort	B-Algorithm
is	O
preferred	O
when	O
the	O
additional	O
performance	O
justifies	O
the	O
implementation	O
effort	O
.	O
</s>
<s>
The	O
other	O
major	O
sorting	B-Algorithm
algorithm	I-Algorithm
is	O
merge	B-Algorithm
sort	I-Algorithm
,	O
but	O
that	O
rarely	O
competes	O
directly	O
with	O
heapsort	B-Application
because	O
it	O
is	O
not	O
in-place	B-Algorithm
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
's	O
requirement	O
for	O
extra	O
space	O
(	O
roughly	O
half	O
the	O
size	O
of	O
the	O
input	O
)	O
is	O
usually	O
prohibitive	O
except	O
in	O
the	O
situations	O
where	O
merge	B-Algorithm
sort	I-Algorithm
has	O
a	O
clear	O
advantage	O
:	O
</s>
<s>
(	O
NOTE	O
,	O
for	O
'	O
Building	O
the	O
Heap	B-Application
 '	O
step	O
:	O
Larger	O
nodes	O
do	O
n't	O
stay	O
below	O
smaller	O
node	O
parents	O
.	O
</s>
<s>
They	O
are	O
swapped	O
with	O
parents	O
,	O
and	O
then	O
recursively	O
checked	O
if	O
another	O
swap	O
is	O
needed	O
,	O
to	O
keep	O
larger	O
numbers	O
above	O
smaller	O
numbers	O
on	O
the	O
heap	B-Application
binary	O
tree	O
.	O
)	O
</s>
