<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
smoothsort	B-Application
is	O
a	O
comparison-based	B-Algorithm
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
A	O
variant	O
of	O
heapsort	B-Application
,	O
it	O
was	O
invented	O
and	O
published	O
by	O
Edsger	O
Dijkstra	O
in	O
1981	O
.	O
</s>
<s>
Like	O
heapsort	B-Application
,	O
smoothsort	B-Application
is	O
an	O
in-place	B-Algorithm
algorithm	I-Algorithm
with	O
an	O
upper	O
bound	O
of	O
,	O
but	O
it	O
is	O
not	O
a	O
stable	O
sort	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>
Like	O
heapsort	B-Application
,	O
smoothsort	B-Application
organizes	O
the	O
input	O
into	O
a	O
priority	B-Application
queue	I-Application
and	O
then	O
repeatedly	O
extracts	O
the	O
maximum	O
.	O
</s>
<s>
Also	O
like	O
heapsort	B-Application
,	O
the	O
priority	B-Application
queue	I-Application
is	O
an	O
implicit	B-Data_Structure
heap	B-Application
data	I-Application
structure	I-Application
(	O
a	O
heap-ordered	O
implicit	B-Data_Structure
binary	O
tree	O
)	O
,	O
which	O
occupies	O
a	O
prefix	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
When	O
the	O
prefix	O
has	O
shrunk	O
to	O
nothing	O
,	O
the	O
array	B-Data_Structure
is	O
completely	O
sorted	O
.	O
</s>
<s>
Heapsort	B-Application
maps	O
the	O
binary	O
tree	O
to	O
the	O
array	B-Data_Structure
using	O
a	O
top-down	O
breadth-first	O
traversal	O
of	O
the	O
tree	O
;	O
the	O
array	B-Data_Structure
begins	O
with	O
the	O
root	O
of	O
the	O
tree	O
,	O
then	O
its	O
two	O
children	O
,	O
then	O
four	O
grandchildren	O
,	O
and	O
so	O
on	O
.	O
</s>
<s>
Every	O
element	O
has	O
a	O
well-defined	O
depth	O
below	O
the	O
root	O
of	O
the	O
tree	O
,	O
and	O
every	O
element	O
except	O
the	O
root	O
has	O
its	O
parent	O
earlier	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Its	O
height	O
above	O
the	O
leaves	O
,	O
however	O
,	O
depends	O
on	O
the	O
size	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
This	O
has	O
the	O
disadvantage	O
that	O
every	O
element	O
must	O
be	O
moved	O
as	O
part	O
of	O
the	O
sorting	B-Algorithm
process	O
:	O
it	O
must	O
pass	O
through	O
the	O
root	O
before	O
being	O
moved	O
to	O
its	O
final	O
location	O
.	O
</s>
<s>
Smoothsort	B-Application
uses	O
a	O
different	O
mapping	O
,	O
a	O
bottom-up	O
depth-first	O
post-order	O
traversal	O
.	O
</s>
<s>
Every	O
element	O
has	O
a	O
well-defined	O
height	O
above	O
the	O
leaves	O
,	O
and	O
every	O
non-leaf	O
element	O
has	O
its	O
children	O
earlier	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Its	O
depth	O
below	O
the	O
root	O
,	O
however	O
,	O
depends	O
on	O
the	O
size	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
The	O
algorithm	O
is	O
organized	O
so	O
the	O
root	O
is	O
at	O
the	O
end	O
of	O
the	O
heap	B-Application
,	O
and	O
at	O
the	O
moment	O
that	O
an	O
element	O
is	O
extracted	O
from	O
the	O
heap	B-Application
it	O
is	O
already	O
in	O
its	O
final	O
location	O
and	O
does	O
not	O
need	O
to	O
be	O
moved	O
.	O
</s>
<s>
Also	O
,	O
a	O
sorted	O
array	B-Data_Structure
is	O
already	O
a	O
valid	O
heap	B-Application
,	O
and	O
many	O
sorted	O
intervals	O
are	O
valid	O
heap-ordered	O
subtrees	O
.	O
</s>
<s>
An	O
initial	O
prefix	O
of	O
the	O
array	B-Data_Structure
(	O
including	O
the	O
whole	O
array	B-Data_Structure
)	O
,	O
might	O
be	O
such	O
an	O
interval	O
corresponding	O
to	O
a	O
subtree	O
,	O
but	O
in	O
general	O
decomposes	O
as	O
a	O
union	O
of	O
a	O
number	O
of	O
successive	O
such	O
subtree	O
intervals	O
,	O
which	O
Dijkstra	O
calls	O
"	O
stretches	O
"	O
.	O
</s>
<s>
The	O
rule	O
Dijkstra	O
uses	O
is	O
that	O
the	O
last	O
two	O
stretches	O
are	O
combined	O
if	O
and	O
only	O
if	O
their	O
sizes	O
are	O
consecutive	O
Leonardo	B-Algorithm
numbers	I-Algorithm
and	O
(	O
in	O
that	O
order	O
)	O
,	O
which	O
numbers	O
are	O
recursively	O
defined	O
,	O
in	O
a	O
manner	O
very	O
similar	O
to	O
the	O
Fibonacci	B-Algorithm
numbers	I-Algorithm
,	O
as	O
:	O
</s>
<s>
As	O
a	O
consequence	O
,	O
the	O
size	O
of	O
any	O
subtree	O
is	O
a	O
Leonardo	B-Algorithm
number	I-Algorithm
.	O
</s>
<s>
The	O
sequence	O
of	O
stretch	O
sizes	O
decomposing	O
the	O
first	O
positions	O
,	O
for	O
any	O
,	O
can	O
be	O
found	O
in	O
a	O
greedy	O
manner	O
:	O
the	O
first	O
size	O
is	O
the	O
largest	O
Leonardo	B-Algorithm
number	I-Algorithm
not	O
exceeding	O
,	O
and	O
the	O
remainder	O
(	O
if	O
any	O
)	O
is	O
decomposed	O
recursively	O
.	O
</s>
<s>
The	O
sizes	O
of	O
stretches	O
are	O
decreasing	O
,	O
strictly	O
so	O
except	O
possibly	O
for	O
two	O
final	O
sizes	O
1	O
,	O
and	O
avoiding	O
successive	O
Leonardo	B-Algorithm
numbers	I-Algorithm
except	O
possibly	O
for	O
the	O
final	O
two	O
sizes	O
.	O
</s>
<s>
In	O
addition	O
to	O
each	O
stretch	O
being	O
a	O
heap-ordered	O
tree	O
,	O
the	O
roots	O
of	O
the	O
trees	O
are	O
maintained	O
in	O
sorted	O
order	O
.	O
</s>
<s>
This	O
combines	O
all	O
of	O
the	O
trees	O
together	O
into	O
one	O
global	O
heap	B-Application
.	O
</s>
<s>
In	O
the	O
first	O
(	O
heap	B-Application
growing	O
)	O
phase	O
of	O
sorting	B-Algorithm
,	O
an	O
increasingly	O
large	O
initial	O
part	O
of	O
the	O
array	B-Data_Structure
is	O
reorganized	O
so	O
that	O
the	O
subtree	O
for	O
each	O
of	O
its	O
stretches	O
is	O
a	O
max-heap	B-Application
:	O
the	O
entry	O
at	O
any	O
non-leaf	O
position	O
is	O
at	O
least	O
as	O
large	O
as	O
the	O
entries	O
at	O
the	O
positions	O
that	O
are	O
its	O
children	O
.	O
</s>
<s>
In	O
the	O
second	O
(	O
heap	B-Application
shrinking	O
)	O
phase	O
,	O
the	O
maximal	O
node	O
is	O
detached	O
from	O
the	O
end	O
of	O
the	O
array	B-Data_Structure
(	O
without	O
needing	O
to	O
move	O
it	O
)	O
and	O
the	O
heap	B-Application
invariants	O
are	O
re-established	O
among	O
its	O
children	O
.	O
</s>
<s>
Practical	O
implementation	O
frequently	O
needs	O
to	O
compute	O
Leonardo	B-Algorithm
numbers	I-Algorithm
.	O
</s>
<s>
Alternatively	O
,	O
if	O
there	O
is	O
a	O
finite	O
bound	O
on	O
the	O
size	O
of	O
arrays	O
to	O
be	O
sorted	O
,	O
a	O
precomputed	O
table	O
of	O
Leonardo	B-Algorithm
numbers	I-Algorithm
can	O
be	O
stored	O
in	O
space	O
.	O
</s>
<s>
"	O
sift	O
down	O
"	O
operation	O
in	O
a	O
binary	O
max-heap	B-Application
.	O
</s>
<s>
The	O
core	O
sift-down	O
operation	O
(	O
which	O
Dijkstra	O
calls	O
"	O
trinkle	O
"	O
)	O
restores	O
the	O
heap	B-Application
invariant	O
when	O
it	O
is	O
possibly	O
violated	O
only	O
at	O
the	O
root	O
node	O
.	O
</s>
<s>
The	O
difference	O
between	O
smoothsort	B-Application
and	O
a	O
binary	O
max-heap	B-Application
is	O
that	O
the	O
root	O
of	O
each	O
stretch	O
must	O
be	O
ordered	O
with	O
respect	O
to	O
a	O
third	O
"	O
stepson	O
"	O
:	O
the	O
root	O
of	O
the	O
preceding	O
stretch	O
.	O
</s>
<s>
There	O
is	O
no	O
need	O
to	O
deal	O
with	O
the	O
special	O
case	O
of	O
one	O
child	O
which	O
occurs	O
in	O
a	O
standard	O
implicit	B-Data_Structure
binary	B-Application
heap	I-Application
.	O
</s>
<s>
When	O
an	O
additional	O
element	O
is	O
considered	O
for	O
incorporation	O
into	O
the	O
sequence	O
of	O
stretches	O
(	O
list	O
of	O
disjoint	O
heap	B-Application
structures	I-Application
)	O
it	O
either	O
forms	O
a	O
new	O
one-element	O
stretch	O
,	O
or	O
it	O
combines	O
the	O
two	O
rightmost	O
stretches	O
by	O
becoming	O
the	O
parent	O
of	O
both	O
their	O
roots	O
and	O
forming	O
a	O
new	O
stretch	O
that	O
replaces	O
the	O
two	O
in	O
the	O
sequence	O
.	O
</s>
<s>
Which	O
of	O
the	O
two	O
happens	O
depends	O
only	O
on	O
the	O
sizes	O
of	O
the	O
stretches	O
currently	O
present	O
(	O
and	O
ultimately	O
only	O
on	O
the	O
index	O
of	O
the	O
element	O
added	O
)	O
;	O
Dijkstra	O
stipulated	O
that	O
stretches	O
are	O
combined	O
if	O
and	O
only	O
if	O
their	O
sizes	O
are	O
and	O
for	O
some	O
,	O
i.e.	O
,	O
consecutive	O
Leonardo	B-Algorithm
numbers	I-Algorithm
;	O
the	O
new	O
stretch	O
will	O
have	O
size	O
.	O
</s>
<s>
In	O
either	O
case	O
,	O
the	O
new	O
element	O
must	O
be	O
sifted	O
down	O
to	O
its	O
correct	O
place	O
in	O
the	O
heap	B-Application
structure	I-Application
.	O
</s>
<s>
Dijkstra	O
's	O
algorithm	O
saves	O
work	O
by	O
observing	O
that	O
the	O
full	O
heap	B-Application
invariant	O
is	O
required	O
at	O
the	O
end	O
of	O
the	O
growing	O
phase	O
,	O
but	O
it	O
is	O
not	O
required	O
at	O
every	O
intermediate	O
step	O
.	O
</s>
<s>
Therefore	O
,	O
while	O
shrinking	O
the	O
heap	B-Application
,	O
the	O
first	O
step	O
of	O
sifting	O
down	O
can	O
be	O
simplified	O
to	O
a	O
single	O
comparison	O
with	O
the	O
stepson	O
.	O
</s>
<s>
Smoothsort	B-Application
takes	O
time	O
to	O
process	O
a	O
presorted	O
array	B-Data_Structure
,	O
in	O
the	O
worst	O
case	O
,	O
and	O
achieves	O
nearly-linear	O
performance	O
on	O
many	O
nearly-sorted	O
inputs	O
.	O
</s>
<s>
Using	O
the	O
count	O
of	O
inversions	O
as	O
a	O
measure	O
of	O
un-sortedness	O
(	O
the	O
number	O
of	O
pairs	O
of	O
indices	O
and	O
with	O
and	O
;	O
for	O
randomly	O
sorted	O
input	O
this	O
is	O
approximately	O
)	O
,	O
there	O
are	O
possible	O
input	O
sequences	O
with	O
inversions	O
which	O
cause	O
it	O
to	O
take	O
time	O
,	O
whereas	O
other	O
adaptive	B-Algorithm
sorting	I-Algorithm
algorithms	O
can	O
solve	O
these	O
cases	O
in	O
time	O
.	O
</s>
<s>
The	O
smoothsort	B-Application
algorithm	O
needs	O
to	O
be	O
able	O
to	O
hold	O
in	O
memory	O
the	O
sizes	O
of	O
all	O
of	O
the	O
trees	O
in	O
the	O
Leonardo	O
heap	B-Application
.	O
</s>
<s>
Since	O
they	O
are	O
sorted	O
by	O
order	O
and	O
all	O
orders	O
are	O
distinct	O
,	O
this	O
is	O
usually	O
done	O
using	O
a	O
bit	B-Data_Structure
vector	I-Data_Structure
indicating	O
which	O
orders	O
are	O
present	O
.	O
</s>
<s>
A	O
simpler	O
algorithm	O
inspired	O
by	O
smoothsort	B-Application
is	O
poplar	O
sort	O
.	O
</s>
<s>
Named	O
after	O
the	O
rows	O
of	O
trees	O
of	O
decreasing	O
size	O
often	O
seen	O
in	O
Dutch	O
polders	B-Algorithm
,	O
it	O
performs	O
fewer	O
comparisons	O
than	O
smoothsort	B-Application
for	O
inputs	O
that	O
are	O
not	O
mostly	O
sorted	O
,	O
but	O
cannot	O
achieve	O
linear	O
time	O
for	O
sorted	O
inputs	O
.	O
</s>
<s>
The	O
significant	O
change	O
made	O
by	O
poplar	O
sort	O
in	O
that	O
the	O
roots	O
of	O
the	O
various	O
trees	O
are	O
not	O
kept	O
in	O
sorted	O
order	O
;	O
there	O
are	O
no	O
"	O
stepson	O
"	O
links	O
tying	O
them	O
together	O
into	O
a	O
single	O
heap	B-Application
.	O
</s>
<s>
Instead	O
,	O
each	O
time	O
the	O
heap	B-Application
is	O
shrunk	O
in	O
the	O
second	O
phase	O
,	O
the	O
roots	O
are	O
searched	O
to	O
find	O
the	O
maximum	O
entry	O
.	O
</s>
<s>
The	O
same	O
structure	O
has	O
been	O
proposed	O
as	O
a	O
general-purpose	O
priority	B-Application
queue	I-Application
under	O
the	O
name	O
post-order	O
heap	B-Application
,	O
achieving	O
amortized	B-General_Concept
insertion	O
time	O
in	O
a	O
structure	O
simpler	O
than	O
an	O
implicit	B-Data_Structure
binomial	B-Application
heap	I-Application
.	O
</s>
<s>
The	O
musl	B-Language
C	O
library	O
uses	O
smoothsort	B-Application
for	O
its	O
implementation	O
of	O
qsort( )	O
.	O
</s>
