<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
a	O
simple	O
sorting	B-Algorithm
algorithm	I-Algorithm
that	O
builds	O
the	O
final	O
sorted	B-Data_Structure
array	I-Data_Structure
(	O
or	O
list	O
)	O
one	O
item	O
at	O
a	O
time	O
by	B-Algorithm
comparisons	I-Algorithm
.	O
</s>
<s>
It	O
is	O
much	O
less	O
efficient	O
on	O
large	O
lists	O
than	O
more	O
advanced	O
algorithms	O
such	O
as	O
quicksort	B-Algorithm
,	O
heapsort	B-Application
,	O
or	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
However	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
provides	O
several	O
advantages	O
:	O
</s>
<s>
Simple	O
implementation	O
:	O
Jon	O
Bentley	O
shows	O
a	O
three-line	O
C/C	O
++	O
version	O
that	O
is	O
five	O
lines	O
when	O
optimized	O
.	O
</s>
<s>
When	O
people	O
manually	O
sort	O
cards	O
in	O
a	O
bridge	O
hand	O
,	O
most	O
use	O
a	O
method	O
that	O
is	O
similar	O
to	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
iterates	B-Algorithm
,	O
consuming	O
one	O
input	O
element	O
each	O
repetition	O
,	O
and	O
grows	O
a	O
sorted	O
output	O
list	O
.	O
</s>
<s>
At	O
each	O
iteration	B-Algorithm
,	O
insertion	B-Algorithm
sort	I-Algorithm
removes	O
one	O
element	O
from	O
the	O
input	O
data	O
,	O
finds	O
the	O
location	O
it	O
belongs	O
within	O
the	O
sorted	B-Algorithm
list	I-Algorithm
,	O
and	O
inserts	O
it	O
there	O
.	O
</s>
<s>
Sorting	B-Algorithm
is	O
typically	O
done	O
in-place	B-Algorithm
,	O
by	O
iterating	O
up	O
the	O
array	B-Data_Structure
,	O
growing	O
the	O
sorted	B-Algorithm
list	I-Algorithm
behind	O
it	O
.	O
</s>
<s>
At	O
each	O
array-position	O
,	O
it	O
checks	O
the	O
value	O
there	O
against	O
the	O
largest	O
value	O
in	O
the	O
sorted	B-Algorithm
list	I-Algorithm
(	O
which	O
happens	O
to	O
be	O
next	O
to	O
it	O
,	O
in	O
the	O
previous	O
array-position	O
checked	O
)	O
.	O
</s>
<s>
If	O
smaller	O
,	O
it	O
finds	O
the	O
correct	O
position	O
within	O
the	O
sorted	B-Algorithm
list	I-Algorithm
,	O
shifts	O
all	O
the	O
larger	O
values	O
up	O
to	O
make	O
a	O
space	O
,	O
and	O
inserts	O
into	O
that	O
correct	O
position	O
.	O
</s>
<s>
The	O
resulting	O
array	B-Data_Structure
after	O
k	O
iterations	B-Algorithm
has	O
the	O
property	O
where	O
the	O
first	O
k	O
+	O
1	O
entries	O
are	O
sorted	O
(	O
"	O
+1	O
"	O
because	O
the	O
first	O
entry	O
is	O
skipped	O
)	O
.	O
</s>
<s>
In	O
each	O
iteration	B-Algorithm
the	O
first	O
remaining	O
entry	O
of	O
the	O
input	O
is	O
removed	O
,	O
and	O
inserted	O
into	O
the	O
result	O
at	O
the	O
correct	O
position	O
,	O
thus	O
extending	O
the	O
result	O
:	O
</s>
<s>
The	O
most	O
common	O
variant	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
which	O
operates	O
on	O
arrays	O
,	O
can	O
be	O
described	O
as	O
follows	O
:	O
</s>
<s>
Suppose	O
there	O
exists	O
a	O
function	O
called	O
Insert	O
designed	O
to	O
insert	O
a	O
value	O
into	O
a	O
sorted	O
sequence	O
at	O
the	O
beginning	O
of	O
an	O
array	B-Data_Structure
.	O
</s>
<s>
The	O
function	O
has	O
the	O
side	O
effect	O
of	O
overwriting	O
the	O
value	O
stored	O
immediately	O
after	O
the	O
sorted	O
sequence	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
To	O
perform	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
begin	O
at	O
the	O
left-most	O
element	O
of	O
the	O
array	B-Data_Structure
and	O
invoke	O
Insert	O
to	O
insert	O
each	O
element	O
encountered	O
into	O
its	O
correct	O
position	O
.	O
</s>
<s>
The	O
ordered	O
sequence	O
into	O
which	O
the	O
element	O
is	O
inserted	O
is	O
stored	O
at	O
the	O
beginning	O
of	O
the	O
array	B-Data_Structure
in	O
the	O
set	O
of	O
indices	O
already	O
examined	O
.	O
</s>
<s>
Pseudocode	B-Language
of	O
the	O
complete	O
algorithm	O
follows	O
,	O
where	O
the	O
arrays	O
are	O
zero-based	O
:	O
</s>
<s>
The	O
outer	O
loop	O
runs	O
over	O
all	O
the	O
elements	O
except	O
the	O
first	O
one	O
,	O
because	O
the	O
single-element	O
prefix	O
A[0:1]	O
is	O
trivially	O
sorted	O
,	O
so	O
the	O
invariant	B-Application
that	O
the	O
first	O
i	O
entries	O
are	O
sorted	O
is	O
true	O
from	O
the	O
start	O
.	O
</s>
<s>
Note	O
that	O
the	O
and-operator	O
in	O
the	O
test	O
must	O
use	O
short-circuit	O
evaluation	O
,	O
otherwise	O
the	O
test	O
might	O
result	O
in	O
an	O
array	B-Data_Structure
bounds	I-Data_Structure
error	I-Data_Structure
,	O
when	O
j	O
=	O
0	O
and	O
it	O
tries	O
to	O
evaluate	O
A[j-1]	O
>	O
A[j]	O
(	O
i.e.	O
</s>
<s>
After	O
expanding	O
the	O
swap	O
operation	O
in-place	B-Algorithm
as	O
x	O
←	O
A[j];	O
A[j]	O
←	O
A[j-1];	O
A[j-1]	O
←	O
x	O
(	O
where	O
x	O
is	O
a	O
temporary	O
variable	O
)	O
,	O
a	O
slightly	O
faster	O
version	O
can	O
be	O
produced	O
that	O
moves	O
A[i]	O
to	O
its	O
position	O
in	O
one	O
go	O
and	O
only	O
performs	O
one	O
assignment	O
in	O
the	O
inner	O
loop	O
body	O
:	O
</s>
<s>
It	O
does	O
not	O
make	O
the	O
code	O
any	O
shorter	O
,	O
it	O
also	O
does	O
n't	O
reduce	O
the	O
execution	O
time	O
,	O
but	O
it	O
increases	O
the	O
additional	O
memory	O
consumption	O
from	O
to	O
(	O
at	O
the	O
deepest	O
level	O
of	O
recursion	O
the	O
stack	O
contains	O
references	O
to	O
the	O
array	B-Data_Structure
,	O
each	O
with	O
accompanying	O
value	O
of	O
variable	O
from	O
down	O
to	O
1	O
)	O
.	O
</s>
<s>
The	O
best	O
case	O
input	O
is	O
an	O
array	B-Data_Structure
that	O
is	O
already	O
sorted	O
.	O
</s>
<s>
In	O
this	O
case	O
insertion	B-Algorithm
sort	I-Algorithm
has	O
a	O
linear	O
running	O
time	O
(	O
i.e.	O
,	O
O(n )	O
)	O
.	O
</s>
<s>
During	O
each	O
iteration	B-Algorithm
,	O
the	O
first	O
remaining	O
element	O
of	O
the	O
input	O
is	O
only	O
compared	O
with	O
the	O
right-most	O
element	O
of	O
the	O
sorted	O
subsection	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
The	O
simplest	O
worst	O
case	O
input	O
is	O
an	O
array	B-Data_Structure
sorted	O
in	O
reverse	O
order	O
.	O
</s>
<s>
In	O
these	O
cases	O
every	O
iteration	B-Algorithm
of	O
the	O
inner	O
loop	O
will	O
scan	O
and	O
shift	O
the	O
entire	O
sorted	O
subsection	O
of	O
the	O
array	B-Data_Structure
before	O
inserting	O
the	O
next	O
element	O
.	O
</s>
<s>
This	O
gives	O
insertion	B-Algorithm
sort	I-Algorithm
a	O
quadratic	O
running	O
time	O
(	O
i.e.	O
,	O
O(n2 )	O
)	O
.	O
</s>
<s>
The	O
average	O
case	O
is	O
also	O
quadratic	O
,	O
which	O
makes	O
insertion	B-Algorithm
sort	I-Algorithm
impractical	O
for	O
sorting	B-Algorithm
large	O
arrays	O
.	O
</s>
<s>
However	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
is	O
one	O
of	O
the	O
fastest	O
algorithms	O
for	O
sorting	B-Algorithm
very	O
small	O
arrays	O
,	O
even	O
faster	O
than	O
quicksort	B-Algorithm
;	O
indeed	O
,	O
good	O
quicksort	B-Algorithm
implementations	O
use	O
insertion	B-Algorithm
sort	I-Algorithm
for	O
arrays	O
smaller	O
than	O
a	O
certain	O
threshold	O
,	O
also	O
when	O
arising	O
as	O
subproblems	O
;	O
the	O
exact	O
threshold	O
must	O
be	O
determined	O
experimentally	O
and	O
depends	O
on	O
the	O
machine	O
,	O
but	O
is	O
commonly	O
around	O
ten	O
.	O
</s>
<s>
Example	O
:	O
The	O
following	O
table	O
shows	O
the	O
steps	O
for	O
sorting	B-Algorithm
the	O
sequence	O
{	O
3	O
,	O
7	O
,	O
4	O
,	O
9	O
,	O
5	O
,	O
2	O
,	O
6	O
,	O
1}	O
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
very	O
similar	O
to	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
As	O
in	O
selection	B-Algorithm
sort	I-Algorithm
,	O
after	O
k	O
passes	O
through	O
the	O
array	B-Data_Structure
,	O
the	O
first	O
k	O
elements	O
are	O
in	O
sorted	O
order	O
.	O
</s>
<s>
However	O
,	O
the	O
fundamental	O
difference	O
between	O
the	O
two	O
algorithms	O
is	O
that	O
insertion	B-Algorithm
sort	I-Algorithm
scans	O
backwards	O
from	O
the	O
current	O
key	O
,	O
while	O
selection	B-Algorithm
sort	I-Algorithm
scans	O
forwards	O
.	O
</s>
<s>
This	O
results	O
in	O
selection	B-Algorithm
sort	I-Algorithm
making	O
the	O
first	O
k	O
elements	O
the	O
k	O
smallest	O
elements	O
of	O
the	O
unsorted	O
input	O
,	O
while	O
in	O
insertion	B-Algorithm
sort	I-Algorithm
they	O
are	O
simply	O
the	O
first	O
k	O
elements	O
of	O
the	O
input	O
.	O
</s>
<s>
The	O
primary	O
advantage	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
over	O
selection	B-Algorithm
sort	I-Algorithm
is	O
that	O
selection	B-Algorithm
sort	I-Algorithm
must	O
always	O
scan	O
all	O
remaining	O
elements	O
to	O
find	O
the	O
absolute	O
smallest	O
element	O
in	O
the	O
unsorted	O
portion	O
of	O
the	O
list	O
,	O
while	O
insertion	B-Algorithm
sort	I-Algorithm
requires	O
only	O
a	O
single	O
comparison	O
when	O
the	O
(	O
k+1	O
)	O
-st	O
element	O
is	O
greater	O
than	O
the	O
k-th	O
element	O
;	O
when	O
this	O
is	O
frequently	O
true	O
(	O
such	O
as	O
if	O
the	O
input	O
array	B-Data_Structure
is	O
already	O
sorted	O
or	O
partially	O
sorted	O
)	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
is	O
distinctly	O
more	O
efficient	O
compared	O
to	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
On	O
average	O
(	O
assuming	O
the	O
rank	O
of	O
the	O
(	O
k+1	O
)	O
-st	O
element	O
rank	O
is	O
random	O
)	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
will	O
require	O
comparing	O
and	O
shifting	O
half	O
of	O
the	O
previous	O
k	O
elements	O
,	O
meaning	O
that	O
insertion	B-Algorithm
sort	I-Algorithm
will	O
perform	O
about	O
half	O
as	O
many	O
comparisons	O
as	O
selection	B-Algorithm
sort	I-Algorithm
on	O
average	O
.	O
</s>
<s>
In	O
the	O
worst	O
case	O
for	O
insertion	B-Algorithm
sort	I-Algorithm
(	O
when	O
the	O
input	O
array	B-Data_Structure
is	O
reverse-sorted	O
)	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
performs	O
just	O
as	O
many	O
comparisons	O
as	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
However	O
,	O
a	O
disadvantage	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
over	O
selection	B-Algorithm
sort	I-Algorithm
is	O
that	O
it	O
requires	O
more	O
writes	O
due	O
to	O
the	O
fact	O
that	O
,	O
on	O
each	O
iteration	B-Algorithm
,	O
inserting	O
the	O
(	O
k+1	O
)	O
-st	O
element	O
into	O
the	O
sorted	O
portion	O
of	O
the	O
array	B-Data_Structure
requires	O
many	O
element	O
swaps	O
to	O
shift	O
all	O
of	O
the	O
following	O
elements	O
,	O
while	O
only	O
a	O
single	O
swap	O
is	O
required	O
for	O
each	O
iteration	B-Algorithm
of	O
selection	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
In	O
general	O
,	O
insertion	B-Algorithm
sort	I-Algorithm
will	O
write	O
to	O
the	O
array	B-Data_Structure
O(n2 )	O
times	O
,	O
whereas	O
selection	B-Algorithm
sort	I-Algorithm
will	O
write	O
only	O
O( )	O
times	O
.	O
</s>
<s>
For	O
this	O
reason	O
selection	B-Algorithm
sort	I-Algorithm
may	O
be	O
preferable	O
in	O
cases	O
where	O
writing	O
to	O
memory	O
is	O
significantly	O
more	O
expensive	O
than	O
reading	O
,	O
such	O
as	O
with	O
EEPROM	B-General_Concept
or	O
flash	B-Device
memory	I-Device
.	O
</s>
<s>
While	O
some	O
divide-and-conquer	B-Algorithm
algorithms	I-Algorithm
such	O
as	O
quicksort	B-Algorithm
and	O
mergesort	B-Algorithm
outperform	O
insertion	B-Algorithm
sort	I-Algorithm
for	O
larger	O
arrays	O
,	O
non-recursive	O
sorting	B-Algorithm
algorithms	I-Algorithm
such	O
as	O
insertion	B-Algorithm
sort	I-Algorithm
or	O
selection	B-Algorithm
sort	I-Algorithm
are	O
generally	O
faster	O
for	O
very	O
small	O
arrays	O
(	O
the	O
exact	O
size	O
varies	O
by	O
environment	O
and	O
implementation	O
,	O
but	O
is	O
typically	O
between	O
7	O
and	O
50	O
elements	O
)	O
.	O
</s>
<s>
Therefore	O
,	O
a	O
useful	O
optimization	O
in	O
the	O
implementation	O
of	O
those	O
algorithms	O
is	O
a	O
hybrid	O
approach	O
,	O
using	O
the	O
simpler	O
algorithm	O
when	O
the	O
array	B-Data_Structure
has	O
been	O
divided	O
to	O
a	O
small	O
size	O
.	O
</s>
<s>
Shell	O
made	O
substantial	O
improvements	O
to	O
the	O
algorithm	O
;	O
the	O
modified	O
version	O
is	O
called	O
Shell	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
sorting	B-Algorithm
algorithm	I-Algorithm
compares	O
elements	O
separated	O
by	O
a	O
distance	O
that	O
decreases	O
on	O
each	O
pass	O
.	O
</s>
<s>
Shell	B-Algorithm
sort	I-Algorithm
has	O
distinctly	O
improved	O
running	O
times	O
in	O
practical	O
work	O
,	O
with	O
two	O
simple	O
variants	O
requiring	O
O( 	O
n3/2	O
)	O
and	O
O( 	O
n4/3	O
)	O
running	O
time	O
.	O
</s>
<s>
If	O
the	O
cost	O
of	O
comparisons	O
exceeds	O
the	O
cost	O
of	O
swaps	O
,	O
as	O
is	O
the	O
case	O
for	O
example	O
with	O
string	O
keys	O
stored	O
by	O
reference	O
or	O
with	O
human	O
interaction	O
(	O
such	O
as	O
choosing	O
one	O
of	O
a	O
pair	O
displayed	O
side-by-side	O
)	O
,	O
then	O
using	O
binary	O
insertion	B-Algorithm
sort	I-Algorithm
may	O
yield	O
better	O
performance	O
.	O
</s>
<s>
Binary	O
insertion	B-Algorithm
sort	I-Algorithm
employs	O
a	O
binary	O
search	O
to	O
determine	O
the	O
correct	O
location	O
to	O
insert	O
new	O
elements	O
,	O
and	O
therefore	O
performs	O
⌈log2n⌉	O
comparisons	O
in	O
the	O
worst	O
case	O
.	O
</s>
<s>
When	O
each	O
element	O
in	O
the	O
array	B-Data_Structure
is	O
searched	O
for	O
and	O
inserted	O
this	O
is	O
O(nlogn )	O
.	O
</s>
<s>
In	O
the	O
extreme	O
case	O
,	O
this	O
variant	O
works	O
similar	O
to	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
A	O
variant	O
named	O
binary	O
merge	B-Algorithm
sort	I-Algorithm
uses	O
a	O
binary	O
insertion	B-Algorithm
sort	I-Algorithm
to	O
sort	O
groups	O
of	O
32	O
elements	O
,	O
followed	O
by	O
a	O
final	O
sort	O
using	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
It	O
combines	O
the	O
speed	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
on	O
small	O
data	O
sets	O
with	O
the	O
speed	O
of	O
merge	B-Algorithm
sort	I-Algorithm
on	O
large	O
data	O
sets	O
.	O
</s>
<s>
To	O
avoid	O
having	O
to	O
make	O
a	O
series	O
of	O
swaps	O
for	O
each	O
insertion	O
,	O
the	O
input	O
could	O
be	O
stored	O
in	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
which	O
allows	O
elements	O
to	O
be	O
spliced	O
into	O
or	O
out	O
of	O
the	O
list	O
in	O
constant	O
time	O
when	O
the	O
position	O
in	O
the	O
list	O
is	O
known	O
.	O
</s>
<s>
However	O
,	O
searching	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
requires	O
sequentially	O
following	O
the	O
links	O
to	O
the	O
desired	O
position	O
:	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
does	O
not	O
have	O
random	O
access	O
,	O
so	O
it	O
cannot	O
use	O
a	O
faster	O
method	O
such	O
as	O
binary	O
search	O
.	O
</s>
<s>
Therefore	O
,	O
the	O
running	O
time	O
required	O
for	O
searching	O
is	O
O(n )	O
,	O
and	O
the	O
time	O
for	O
sorting	B-Algorithm
is	O
O(n2 )	O
.	O
</s>
<s>
If	O
a	O
more	O
sophisticated	O
data	B-General_Concept
structure	I-General_Concept
(	O
e.g.	O
,	O
heap	B-Application
or	O
binary	O
tree	O
)	O
is	O
used	O
,	O
the	O
time	O
required	O
for	O
searching	O
and	O
insertion	O
can	O
be	O
reduced	O
significantly	O
;	O
this	O
is	O
the	O
essence	O
of	O
heap	B-Application
sort	I-Application
and	O
binary	B-Algorithm
tree	I-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
In	O
2006	O
Bender	O
,	O
Martin	O
Farach-Colton	O
,	O
and	O
Mosteiro	O
published	O
a	O
new	O
variant	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
called	O
library	B-Algorithm
sort	I-Algorithm
or	O
gapped	B-Algorithm
insertion	I-Algorithm
sort	I-Algorithm
that	O
leaves	O
a	O
small	O
number	O
of	O
unused	O
spaces	O
(	O
i.e.	O
,	O
"	O
gaps	O
"	O
)	O
spread	O
throughout	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
The	O
authors	O
show	O
that	O
this	O
sorting	B-Algorithm
algorithm	I-Algorithm
runs	O
with	O
high	O
probability	O
in	O
O(nlogn )	O
time	O
.	O
</s>
<s>
If	O
a	O
skip	O
list	O
is	O
used	O
,	O
the	O
insertion	O
time	O
is	O
brought	O
down	O
to	O
O(logn )	O
,	O
and	O
swaps	O
are	O
not	O
needed	O
because	O
the	O
skip	O
list	O
is	O
implemented	O
on	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
structure	O
.	O
</s>
<s>
If	O
the	O
items	O
are	O
stored	O
in	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
,	O
then	O
the	O
list	O
can	O
be	O
sorted	O
with	O
O(1 )	O
additional	O
space	O
.	O
</s>
<s>
The	O
input	O
items	O
are	O
taken	O
off	O
the	O
list	O
one	O
at	O
a	O
time	O
,	O
and	O
then	O
inserted	O
in	O
the	O
proper	O
place	O
in	O
the	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
When	O
the	O
input	O
list	O
is	O
empty	O
,	O
the	O
sorted	B-Algorithm
list	I-Algorithm
has	O
the	O
desired	O
result	O
.	O
</s>
<s>
The	O
algorithm	O
below	O
uses	O
a	O
trailing	O
pointer	O
for	O
the	O
insertion	O
into	O
the	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
