<s>
A	O
tree	B-Algorithm
sort	I-Algorithm
is	O
a	O
sort	B-Algorithm
algorithm	I-Algorithm
that	O
builds	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
from	O
the	O
elements	O
to	O
be	O
sorted	O
,	O
and	O
then	O
traverses	O
the	O
tree	O
(	O
in-order	B-Algorithm
)	O
so	O
that	O
the	O
elements	O
come	O
out	O
in	O
sorted	O
order	O
.	O
</s>
<s>
Its	O
typical	O
use	O
is	O
sorting	B-Algorithm
elements	O
online	B-Algorithm
:	O
after	O
each	O
insertion	O
,	O
the	O
set	O
of	O
elements	O
seen	O
so	O
far	O
is	O
available	O
in	O
sorted	O
order	O
.	O
</s>
<s>
Tree	B-Algorithm
sort	I-Algorithm
can	O
be	O
used	O
as	O
a	O
one-time	O
sort	O
,	O
but	O
it	O
is	O
equivalent	O
to	O
quicksort	B-Algorithm
as	O
both	O
recursively	O
partition	O
the	O
elements	O
based	O
on	O
a	O
pivot	O
,	O
and	O
since	O
quicksort	B-Algorithm
is	O
in-place	O
and	O
has	O
lower	O
overhead	O
,	O
tree	B-Algorithm
sort	I-Algorithm
has	O
few	O
advantages	O
over	O
quicksort	B-Algorithm
.	O
</s>
<s>
Adding	O
one	O
item	O
to	O
a	O
binary	B-Language
search	I-Language
tree	I-Language
is	O
on	O
average	O
an	O
process	O
(	O
in	O
big	O
O	O
notation	O
)	O
.	O
</s>
<s>
Adding	O
n	O
items	O
is	O
an	O
process	O
,	O
making	O
tree	O
sorting	B-Algorithm
a	O
'	O
fast	O
sort	O
 '	O
process	O
.	O
</s>
<s>
Adding	O
an	O
item	O
to	O
an	O
unbalanced	O
binary	O
tree	O
requires	O
time	O
in	O
the	O
worst-case	O
:	O
When	O
the	O
tree	O
resembles	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
(	O
degenerate	O
tree	O
)	O
.	O
</s>
<s>
This	O
results	O
in	O
a	O
worst	O
case	O
of	O
time	O
for	O
this	O
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Expected	O
time	O
can	O
however	O
be	O
achieved	O
by	O
shuffling	O
the	O
array	B-Data_Structure
,	O
but	O
this	O
does	O
not	O
help	O
for	O
equal	O
items	O
.	O
</s>
<s>
The	O
worst-case	O
behaviour	O
can	O
be	O
improved	O
by	O
using	O
a	O
self-balancing	B-Data_Structure
binary	I-Data_Structure
search	I-Data_Structure
tree	I-Data_Structure
.	O
</s>
<s>
Using	O
such	O
a	O
tree	O
,	O
the	O
algorithm	O
has	O
an	O
worst-case	O
performance	O
,	O
thus	O
being	O
degree-optimal	O
for	O
a	O
comparison	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
However	O
,	O
tree	B-Algorithm
sort	I-Algorithm
algorithms	O
require	O
separate	O
memory	O
to	O
be	O
allocated	O
for	O
the	O
tree	O
,	O
as	O
opposed	O
to	O
in-place	O
algorithms	O
such	O
as	O
quicksort	B-Algorithm
or	O
heapsort	B-Application
.	O
</s>
<s>
On	O
most	O
common	O
platforms	O
,	O
this	O
means	O
that	O
heap	O
memory	O
has	O
to	O
be	O
used	O
,	O
which	O
is	O
a	O
significant	O
performance	O
hit	O
when	O
compared	O
to	O
quicksort	B-Algorithm
and	O
heapsort	B-Application
.	O
</s>
<s>
When	O
using	O
a	O
splay	B-Data_Structure
tree	I-Data_Structure
as	O
the	O
binary	B-Language
search	I-Language
tree	I-Language
,	O
the	O
resulting	O
algorithm	O
(	O
called	O
splaysort	B-Algorithm
)	O
has	O
the	O
additional	O
property	O
that	O
it	O
is	O
an	O
adaptive	B-Algorithm
sort	I-Algorithm
,	O
meaning	O
that	O
its	O
running	O
time	O
is	O
faster	O
than	O
for	O
inputs	O
that	O
are	O
nearly	O
sorted	O
.	O
</s>
<s>
The	O
following	O
tree	B-Algorithm
sort	I-Algorithm
algorithm	O
in	O
pseudocode	O
accepts	O
a	O
collection	O
of	O
comparable	O
items	O
and	O
outputs	O
the	O
items	O
in	O
ascending	O
order	O
:	O
</s>
<s>
In	O
a	O
simple	O
functional	B-Language
programming	I-Language
form	O
,	O
the	O
algorithm	O
(	O
in	O
Haskell	B-Language
)	O
would	O
look	O
something	O
like	O
this	O
:	O
</s>
