<s>
Timsort	B-Algorithm
is	O
a	O
hybrid	B-Algorithm
,	O
stable	O
sorting	B-Algorithm
algorithm	I-Algorithm
,	O
derived	O
from	O
merge	B-Algorithm
sort	I-Algorithm
and	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
designed	O
to	O
perform	O
well	O
on	O
many	O
kinds	O
of	O
real-world	O
data	O
.	O
</s>
<s>
It	O
was	O
implemented	O
by	O
Tim	O
Peters	O
in	O
2002	O
for	O
use	O
in	O
the	O
Python	B-Language
programming	I-Language
language	I-Language
.	O
</s>
<s>
Timsort	B-Algorithm
has	O
been	O
Python	B-Language
's	O
standard	O
sorting	B-Algorithm
algorithm	I-Algorithm
since	O
version	O
2.3	O
.	O
</s>
<s>
It	O
is	O
also	O
used	O
to	O
sort	O
arrays	O
of	O
non-primitive	O
type	O
in	O
Java	O
SE	O
7	O
,	O
on	O
the	O
Android	B-Application
platform	I-Application
,	O
in	O
GNU	B-Language
Octave	I-Language
,	O
on	O
V8	B-Language
,	O
Swift	B-Application
,	O
and	O
Rust	B-Application
.	O
</s>
<s>
It	O
uses	O
techniques	O
from	O
Peter	O
McIlroy	O
's	O
1993	O
paper	O
"	O
Optimistic	O
Sorting	B-Algorithm
and	O
Information	O
Theoretic	O
Complexity	O
"	O
.	O
</s>
<s>
Timsort	B-Algorithm
was	O
designed	O
to	O
take	O
advantage	O
of	O
runs	O
of	O
consecutive	O
ordered	O
elements	O
that	O
already	O
exist	O
in	O
most	O
real-world	O
data	O
,	O
natural	O
runs	O
.	O
</s>
<s>
It	O
iterates	O
over	O
the	O
data	O
collecting	O
elements	O
into	O
runs	O
and	O
simultaneously	O
putting	O
those	O
runs	O
in	O
a	O
stack	B-Application
.	O
</s>
<s>
Whenever	O
the	O
runs	O
on	O
the	O
top	O
of	O
the	O
stack	B-Application
match	O
a	O
merge	O
criterion	O
,	O
they	O
are	O
merged	O
.	O
</s>
<s>
The	O
advantage	O
of	O
merging	O
ordered	O
runs	O
instead	O
of	O
merging	O
fixed	O
size	O
sub-lists	O
(	O
as	O
done	O
by	O
traditional	O
mergesort	B-Algorithm
)	O
is	O
that	O
it	O
decreases	O
the	O
total	O
number	O
of	O
comparisons	O
needed	O
to	O
sort	O
the	O
entire	O
list	O
.	O
</s>
<s>
If	O
a	O
run	O
is	O
smaller	O
than	O
this	O
minimum	O
run	O
size	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
is	O
used	O
to	O
add	O
more	O
elements	O
to	O
the	O
run	O
until	O
the	O
minimum	O
run	O
size	O
is	O
reached	O
.	O
</s>
<s>
Timsort	B-Algorithm
is	O
a	O
stable	O
sorting	B-Algorithm
algorithm	I-Algorithm
(	O
order	O
of	O
elements	O
with	O
same	O
key	O
is	O
kept	O
)	O
and	O
strives	O
to	O
perform	O
balanced	O
merges	O
(	O
a	O
merge	O
thus	O
merges	O
runs	O
of	O
similar	O
sizes	O
)	O
.	O
</s>
<s>
In	O
order	O
to	O
achieve	O
sorting	B-Algorithm
stability	O
,	O
only	O
consecutive	O
runs	O
are	O
merged	O
.	O
</s>
<s>
In	O
pursuit	O
of	O
balanced	O
merges	O
,	O
Timsort	B-Algorithm
considers	O
three	O
runs	O
on	O
the	O
top	O
of	O
the	O
stack	B-Application
,	O
X	O
,	O
Y	O
,	O
Z	O
,	O
and	O
maintains	O
the	O
invariants	O
:	O
</s>
<s>
These	O
invariants	O
maintain	O
merges	O
as	O
being	O
approximately	O
balanced	O
while	O
maintaining	O
a	O
compromise	O
between	O
delaying	O
merging	O
for	O
balance	O
,	O
exploiting	O
fresh	O
occurrence	O
of	O
runs	O
in	O
cache	B-General_Concept
memory	I-General_Concept
and	O
making	O
merge	O
decisions	O
relatively	O
simple	O
.	O
</s>
<s>
The	O
original	O
merge	B-Algorithm
sort	I-Algorithm
implementation	O
is	O
not	O
in-place	O
and	O
it	O
has	O
a	O
space	O
overhead	O
of	O
N	O
(	O
data	O
size	O
)	O
.	O
</s>
<s>
In-place	O
merge	B-Algorithm
sort	I-Algorithm
implementations	O
exist	O
,	O
but	O
have	O
a	O
high	O
time	O
overhead	O
.	O
</s>
<s>
In	O
order	O
to	O
achieve	O
a	O
middle	O
term	O
,	O
Timsort	B-Algorithm
performs	O
a	O
merge	B-Algorithm
sort	I-Algorithm
with	O
a	O
small	O
time	O
overhead	O
and	O
smaller	O
space	O
overhead	O
than	O
N	O
.	O
</s>
<s>
First	O
,	O
Timsort	B-Algorithm
performs	O
a	O
binary	O
search	O
to	O
find	O
the	O
location	O
where	O
the	O
first	O
element	O
of	O
the	O
second	O
run	O
would	O
be	O
inserted	O
in	O
the	O
first	O
ordered	O
run	O
,	O
keeping	O
it	O
ordered	O
.	O
</s>
<s>
Merging	O
can	O
be	O
done	O
in	O
both	O
directions	O
:	O
left-to-right	O
,	O
as	O
in	O
the	O
traditional	O
mergesort	B-Algorithm
,	O
or	O
right-to-left	O
.	O
</s>
<s>
When	O
this	O
number	O
reaches	O
the	O
minimum	O
galloping	O
threshold	O
(	O
min_gallop	O
)	O
,	O
Timsort	B-Algorithm
considers	O
that	O
it	O
is	O
likely	O
that	O
many	O
consecutive	O
elements	O
may	O
still	O
be	O
selected	O
from	O
that	O
run	O
and	O
switches	O
to	O
the	O
galloping	O
mode	O
.	O
</s>
<s>
In	O
this	O
mode	O
,	O
the	O
algorithm	O
performs	O
an	O
exponential	B-Algorithm
search	I-Algorithm
,	O
also	O
known	O
as	O
galloping	O
search	O
,	O
for	O
the	O
next	O
element	O
x	O
of	O
the	O
run	O
R2	O
in	O
the	O
run	O
R1	O
.	O
</s>
<s>
In	O
some	O
cases	O
galloping	O
mode	O
requires	O
more	O
comparisons	O
than	O
a	O
simple	O
linear	B-Algorithm
search	I-Algorithm
.	O
</s>
<s>
If	O
the	O
selected	O
element	O
is	O
from	O
the	O
same	O
array	B-Data_Structure
that	O
returned	O
an	O
element	O
previously	O
,	O
min_gallop	O
is	O
reduced	O
by	O
one	O
,	O
thus	O
encouraging	O
the	O
return	O
to	O
galloping	O
mode	O
.	O
</s>
<s>
In	O
order	O
to	O
also	O
take	O
advantage	O
of	O
data	O
sorted	O
in	O
descending	O
order	O
,	O
Timsort	B-Algorithm
reverses	O
strictly	O
descending	O
runs	O
when	O
it	O
finds	O
them	O
and	O
adds	O
them	O
to	O
the	O
stack	B-Application
of	O
runs	O
.	O
</s>
<s>
Because	O
merging	O
is	O
most	O
efficient	O
when	O
the	O
number	O
of	O
runs	O
is	O
equal	O
to	O
,	O
or	O
slightly	O
less	O
than	O
,	O
a	O
power	O
of	O
two	O
,	O
and	O
notably	O
less	O
efficient	O
when	O
the	O
number	O
of	O
runs	O
is	O
slightly	O
more	O
than	O
a	O
power	O
of	O
two	O
,	O
Timsort	B-Algorithm
chooses	O
minrun	O
to	O
try	O
to	O
ensure	O
the	O
former	O
condition	O
.	O
</s>
<s>
The	O
final	O
algorithm	O
takes	O
the	O
six	O
most	O
significant	O
bits	O
of	O
the	O
size	O
of	O
the	O
array	B-Data_Structure
,	O
adds	O
one	O
if	O
any	O
of	O
the	O
remaining	O
bits	O
are	O
set	O
,	O
and	O
uses	O
that	O
result	O
as	O
the	O
minrun	O
.	O
</s>
<s>
This	O
algorithm	O
works	O
for	O
all	O
arrays	O
,	O
including	O
those	O
smaller	O
than	O
64	O
;	O
for	O
arrays	O
of	O
size	O
63	O
or	O
less	O
,	O
this	O
sets	O
minrun	O
equal	O
to	O
the	O
array	B-Data_Structure
size	O
and	O
Timsort	B-Algorithm
reduces	O
to	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
In	O
the	O
worst	B-General_Concept
case	I-General_Concept
,	O
Timsort	B-Algorithm
takes	O
comparisons	O
to	O
sort	O
an	O
array	B-Data_Structure
of	O
elements	O
.	O
</s>
<s>
In	O
the	O
best	B-General_Concept
case	I-General_Concept
,	O
which	O
occurs	O
when	O
the	O
input	O
is	O
already	O
sorted	O
,	O
it	O
runs	O
in	O
linear	O
time	O
,	O
meaning	O
that	O
it	O
is	O
an	O
adaptive	O
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
It	O
is	O
superior	O
to	O
Quicksort	B-Algorithm
for	O
sorting	B-Algorithm
object	O
references	O
or	O
pointers	O
because	O
these	O
require	O
expensive	O
memory	O
indirection	O
to	O
access	O
data	O
and	O
perform	O
comparisons	O
and	O
Quicksort	B-Algorithm
's	O
cache	O
coherence	O
benefits	O
are	O
greatly	O
reduced	O
.	O
</s>
<s>
In	O
2015	O
,	O
Dutch	O
and	O
German	O
researchers	O
in	O
the	O
EU	O
FP7	O
ENVISAGE	O
project	O
found	O
a	O
bug	O
in	O
the	O
standard	O
implementation	O
of	O
Timsort	B-Algorithm
.	O
</s>
<s>
It	O
was	O
fixed	O
in	O
2015	O
in	O
Python	B-Language
,	O
Java	O
and	O
Android	B-Application
.	O
</s>
<s>
Specifically	O
,	O
the	O
invariants	O
on	O
stacked	O
run	O
sizes	O
ensure	O
a	O
tight	O
upper	O
bound	O
on	O
the	O
maximum	O
size	O
of	O
the	O
required	O
stack	B-Application
.	O
</s>
<s>
The	O
implementation	O
preallocated	O
a	O
stack	B-Application
sufficient	O
to	O
sort	O
264	O
bytes	O
of	O
input	O
,	O
and	O
avoided	O
further	O
overflow	O
checks	O
.	O
</s>
<s>
Using	O
the	O
KeY	O
tool	O
for	O
formal	O
verification	O
of	O
Java	O
software	O
,	O
the	O
researchers	O
found	O
that	O
this	O
check	O
is	O
not	O
sufficient	O
,	O
and	O
they	O
were	O
able	O
to	O
find	O
run	O
lengths	O
(	O
and	O
inputs	O
which	O
generated	O
those	O
run	O
lengths	O
)	O
which	O
would	O
result	O
in	O
the	O
invariants	O
being	O
violated	O
deeper	O
in	O
the	O
stack	B-Application
after	O
the	O
top	O
of	O
the	O
stack	B-Application
was	O
merged	O
.	O
</s>
<s>
In	O
Java	O
,	O
this	O
generates	O
for	O
those	O
inputs	O
an	O
array-out-of-bound	O
exception	O
.	O
</s>
<s>
The	O
smallest	O
input	O
that	O
triggers	O
this	O
exception	O
in	O
Java	O
and	O
Android	B-Application
v7	O
is	O
of	O
size	O
(	O
226	O
)	O
.	O
</s>
<s>
The	O
Java	O
implementation	O
was	O
corrected	O
by	O
increasing	O
the	O
size	O
of	O
the	O
preallocated	O
stack	B-Application
based	O
on	O
an	O
updated	O
worst-case	B-General_Concept
analysis	O
.	O
</s>
<s>
The	O
article	O
also	O
showed	O
by	O
formal	O
methods	O
how	O
to	O
establish	O
the	O
intended	O
invariant	O
by	O
checking	O
that	O
the	O
four	O
topmost	O
runs	O
in	O
the	O
stack	B-Application
satisfy	O
the	O
two	O
rules	O
above	O
.	O
</s>
<s>
This	O
approach	O
was	O
adopted	O
by	O
Python	B-Language
and	O
Android	B-Application
.	O
</s>
