<s>
Shellsort	B-Algorithm
,	O
also	O
known	O
as	O
Shell	B-Algorithm
sort	I-Algorithm
or	O
Shell	B-Algorithm
's	I-Algorithm
method	I-Algorithm
,	O
is	O
an	O
in-place	B-Algorithm
comparison	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
It	O
can	O
be	O
seen	O
as	O
either	O
a	O
generalization	O
of	O
sorting	B-Algorithm
by	O
exchange	O
(	O
bubble	B-Algorithm
sort	I-Algorithm
)	O
or	O
sorting	B-Algorithm
by	O
insertion	O
(	O
insertion	B-Algorithm
sort	I-Algorithm
)	O
.	O
</s>
<s>
The	O
method	O
starts	O
by	O
sorting	B-Algorithm
pairs	O
of	O
elements	O
far	O
apart	O
from	O
each	O
other	O
,	O
then	O
progressively	O
reducing	O
the	O
gap	O
between	O
elements	O
to	O
be	O
compared	O
.	O
</s>
<s>
The	O
running	O
time	O
of	O
Shellsort	B-Algorithm
is	O
heavily	O
dependent	O
on	O
the	O
gap	O
sequence	O
it	O
uses	O
.	O
</s>
<s>
Shellsort	B-Algorithm
is	O
an	O
optimization	O
of	O
insertion	B-Algorithm
sort	I-Algorithm
that	O
allows	O
the	O
exchange	O
of	O
items	O
that	O
are	O
far	O
apart	O
.	O
</s>
<s>
The	O
idea	O
is	O
to	O
arrange	O
the	O
list	O
of	O
elements	O
so	O
that	O
,	O
starting	O
anywhere	O
,	O
taking	O
every	O
hth	O
element	O
produces	O
a	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
Following	O
this	O
idea	O
for	O
a	O
decreasing	O
sequence	O
of	O
h	O
values	O
ending	O
in	O
1	O
is	O
guaranteed	O
to	O
leave	O
a	O
sorted	B-Algorithm
list	I-Algorithm
in	O
the	O
end	O
.	O
</s>
<s>
In	O
simplistic	O
terms	O
,	O
this	O
means	O
if	O
we	O
have	O
an	O
array	B-Data_Structure
of	O
1024	O
numbers	O
,	O
our	O
first	O
gap	O
(	O
h	O
)	O
could	O
be	O
512	O
.	O
</s>
<s>
Our	O
second	O
gap	O
(	O
k	O
)	O
is	O
256	O
,	O
which	O
breaks	O
the	O
array	B-Data_Structure
into	O
four	O
sections	O
(	O
starting	O
at	O
0,256,512,768	O
)	O
,	O
and	O
we	O
make	O
sure	O
the	O
first	O
items	O
in	O
each	O
section	O
are	O
sorted	O
relative	O
to	O
each	O
other	O
,	O
then	O
the	O
second	O
item	O
in	O
each	O
section	O
,	O
and	O
so	O
on	O
.	O
</s>
<s>
In	O
practice	O
the	O
gap	O
sequence	O
could	O
be	O
anything	O
,	O
but	O
the	O
last	O
gap	O
is	O
always	O
1	O
to	O
finish	O
the	O
sort	O
(	O
effectively	O
finishing	O
with	O
an	O
ordinary	O
insertion	B-Algorithm
sort	I-Algorithm
)	O
.	O
</s>
<s>
An	O
example	O
run	O
of	O
Shellsort	B-Algorithm
with	O
gaps	O
5	O
,	O
3	O
and	O
1	O
is	O
shown	O
below	O
.	O
</s>
<s>
The	O
first	O
pass	O
,	O
5-sorting	O
,	O
performs	O
insertion	B-Algorithm
sort	I-Algorithm
on	O
five	O
separate	O
subarrays	O
(	O
a1	O
,	O
a6	O
,	O
a11	O
)	O
,	O
(	O
a2	O
,	O
a7	O
,	O
a12	O
)	O
,	O
(	O
a3	O
,	O
a8	O
)	O
,	O
(	O
a4	O
,	O
a9	O
)	O
,	O
(	O
a5	O
,	O
a10	O
)	O
.	O
</s>
<s>
The	O
next	O
pass	O
,	O
3-sorting	O
,	O
performs	O
insertion	B-Algorithm
sort	I-Algorithm
on	O
the	O
three	O
subarrays	O
(	O
a1	O
,	O
a4	O
,	O
a7	O
,	O
a10	O
)	O
,	O
(	O
a2	O
,	O
a5	O
,	O
a8	O
,	O
a11	O
)	O
,	O
(	O
a3	O
,	O
a6	O
,	O
a9	O
,	O
a12	O
)	O
.	O
</s>
<s>
The	O
last	O
pass	O
,	O
1-sorting	O
,	O
is	O
an	O
ordinary	O
insertion	B-Algorithm
sort	I-Algorithm
of	O
the	O
entire	O
array	B-Data_Structure
(	O
a1	O
,...,	O
a12	O
)	O
.	O
</s>
<s>
As	O
the	O
example	O
illustrates	O
,	O
the	O
subarrays	O
that	O
Shellsort	B-Algorithm
operates	O
on	O
are	O
initially	O
short	O
;	O
later	O
they	O
are	O
longer	O
but	O
almost	O
ordered	O
.	O
</s>
<s>
In	O
both	O
cases	O
insertion	B-Algorithm
sort	I-Algorithm
works	O
efficiently	O
.	O
</s>
<s>
Shellsort	B-Algorithm
is	O
not	O
stable	O
:	O
it	O
may	O
change	O
the	O
relative	O
order	O
of	O
elements	O
with	O
equal	O
values	O
.	O
</s>
<s>
It	O
is	O
an	O
adaptive	B-Algorithm
sorting	I-Algorithm
algorithm	I-Algorithm
in	O
that	O
it	O
executes	O
faster	O
when	O
the	O
input	O
is	O
partially	O
sorted	O
.	O
</s>
<s>
Using	O
Marcin	O
Ciura	O
's	O
gap	O
sequence	O
,	O
with	O
an	O
inner	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
Every	O
gap	O
sequence	O
that	O
contains	O
1	O
yields	O
a	O
correct	O
sort	O
(	O
as	O
this	O
makes	O
the	O
final	O
pass	O
an	O
ordinary	O
insertion	B-Algorithm
sort	I-Algorithm
)	O
;	O
however	O
,	O
the	O
properties	O
of	O
thus	O
obtained	O
versions	O
of	O
Shellsort	B-Algorithm
may	O
be	O
very	O
different	O
.	O
</s>
<s>
Some	O
of	O
them	O
have	O
decreasing	O
elements	O
that	O
depend	O
on	O
the	O
size	O
of	O
the	O
sorted	O
array	B-Data_Structure
(	O
N	O
)	O
.	O
</s>
<s>
When	O
the	O
binary	O
representation	O
of	O
N	O
contains	O
many	O
consecutive	O
zeroes	O
,	O
Shellsort	B-Algorithm
using	O
Shell	O
's	O
original	O
gap	O
sequence	O
makes	O
Θ(N2 )	O
comparisons	O
in	O
the	O
worst	O
case	O
.	O
</s>
<s>
Although	O
it	O
has	O
higher	O
complexity	O
than	O
the	O
O(NlogN )	O
that	O
is	O
optimal	O
for	O
comparison	B-Algorithm
sorts	I-Algorithm
,	O
Pratt	O
's	O
version	O
lends	O
itself	O
to	O
sorting	B-Algorithm
networks	I-Algorithm
and	O
has	O
the	O
same	O
asymptotic	O
gate	O
complexity	O
as	O
Batcher	O
's	O
bitonic	B-Algorithm
sorter	I-Algorithm
.	O
</s>
<s>
Gonnet	O
and	O
Baeza-Yates	O
observed	O
that	O
Shellsort	B-Algorithm
makes	O
the	O
fewest	O
comparisons	O
on	O
average	O
when	O
the	O
ratios	O
of	O
successive	O
gaps	O
are	O
roughly	O
equal	O
to	O
2.2	O
.	O
</s>
<s>
If	O
the	O
maximum	O
input	O
size	O
is	O
small	O
,	O
as	O
may	O
occur	O
if	O
Shellsort	B-Algorithm
is	O
used	O
on	O
small	O
subarrays	O
by	O
another	O
recursive	O
sorting	B-Algorithm
algorithm	I-Algorithm
such	O
as	O
quicksort	B-Algorithm
or	O
merge	B-Algorithm
sort	I-Algorithm
,	O
then	O
it	O
is	O
possible	O
to	O
tabulate	O
an	O
optimal	O
sequence	O
for	O
each	O
input	O
size	O
.	O
</s>
<s>
The	O
following	O
property	O
holds	O
:	O
after	O
h2-sorting	O
of	O
any	O
h1-sorted	O
array	B-Data_Structure
,	O
the	O
array	B-Data_Structure
remains	O
h1-sorted	O
.	O
</s>
<s>
Every	O
h1-sorted	O
and	O
h2-sorted	O
array	B-Data_Structure
is	O
also	O
(	O
a1h1+a2h2	O
)	O
-sorted	O
,	O
for	O
any	O
nonnegative	O
integers	O
a1	O
and	O
a2	O
.	O
</s>
<s>
The	O
worst-case	O
complexity	O
of	O
Shellsort	B-Algorithm
is	O
therefore	O
connected	O
with	O
the	O
Frobenius	O
problem	O
:	O
for	O
given	O
integers	O
h1	O
,...,	O
hn	O
with	O
gcd	O
=	O
1	O
,	O
the	O
Frobenius	O
number	O
g( h1	O
,...,	O
hn	O
)	O
is	O
the	O
greatest	O
integer	O
that	O
cannot	O
be	O
represented	O
as	O
a1h1+	O
...	O
+anhn	O
with	O
nonnegative	O
integer	O
a1	O
,...,	O
an	O
.	O
</s>
<s>
Using	O
known	O
formulae	O
for	O
Frobenius	O
numbers	O
,	O
we	O
can	O
determine	O
the	O
worst-case	O
complexity	O
of	O
Shellsort	B-Algorithm
for	O
several	O
classes	O
of	O
gap	O
sequences	O
.	O
</s>
<s>
Mark	O
Allen	O
Weiss	O
proved	O
that	O
Shellsort	B-Algorithm
runs	O
in	O
O(N log N )	O
time	O
when	O
the	O
input	O
array	B-Data_Structure
is	O
in	O
reverse	O
order	O
.	O
</s>
<s>
Knuth	O
determined	O
the	O
average	O
complexity	O
of	O
sorting	B-Algorithm
an	O
N-element	O
array	B-Data_Structure
with	O
two	O
gaps	O
(	O
h	O
,	O
1	O
)	O
to	O
be	O
.	O
</s>
<s>
It	O
follows	O
that	O
a	O
two-pass	O
Shellsort	B-Algorithm
with	O
h	O
=	O
Θ( 	O
N1/3	O
)	O
makes	O
on	O
average	O
O( 	O
N5/3	O
)	O
comparisons/inversions/running	O
time	O
.	O
</s>
<s>
Yao	O
found	O
the	O
average	O
complexity	O
of	O
a	O
three-pass	O
Shellsort	B-Algorithm
.	O
</s>
<s>
His	O
result	O
was	O
refined	O
by	O
Janson	O
and	O
Knuth	O
:	O
the	O
average	O
number	O
of	O
comparisons/inversions/running	O
time	O
made	O
during	O
a	O
Shellsort	B-Algorithm
with	O
three	O
gaps	O
(	O
ch	O
,	O
cg	O
,	O
1	O
)	O
,	O
where	O
h	O
and	O
g	O
are	O
coprime	O
,	O
is	O
in	O
the	O
first	O
pass	O
,	O
in	O
the	O
second	O
pass	O
and	O
in	O
the	O
third	O
pass	O
.	O
</s>
<s>
In	O
particular	O
,	O
when	O
h	O
=	O
Θ( 	O
N7/15	O
)	O
and	O
g	O
=	O
Θ( 	O
N1/5	O
)	O
,	O
the	O
average	O
time	O
of	O
sorting	B-Algorithm
is	O
O( 	O
N23/15	O
)	O
.	O
</s>
<s>
Based	O
on	O
experiments	O
,	O
it	O
is	O
conjectured	O
that	O
Shellsort	B-Algorithm
with	O
Hibbard	O
's	O
gap	O
sequence	O
runs	O
in	O
O( 	O
N5/4	O
)	O
average	O
time	O
,	O
and	O
that	O
Gonnet	O
and	O
Baeza-Yates	O
'	O
s	O
sequence	O
requires	O
on	O
average	O
0.41NlnN	O
( lnlnN+	O
1/6	O
)	O
element	O
moves	O
.	O
</s>
<s>
The	O
graph	O
below	O
shows	O
the	O
average	O
number	O
of	O
element	O
comparisons	O
in	O
various	O
variants	O
of	O
Shellsort	B-Algorithm
,	O
divided	O
by	O
the	O
theoretical	O
lower	O
bound	O
,	O
i.e.	O
</s>
<s>
proved	O
the	O
following	O
lower	O
bound	O
for	O
the	O
order	O
of	O
the	O
average	O
number	O
of	O
operations/running	O
time	O
in	O
a	O
p-pass	O
Shellsort	B-Algorithm
:	O
Ω( pN1+	O
1/p	O
)	O
when	O
p≤log2N	O
and	O
Ω(pN )	O
when	O
p>log2N	O
.	O
</s>
<s>
Therefore	O
,	O
Shellsort	B-Algorithm
has	O
prospects	O
of	O
running	O
in	O
an	O
average	O
time	O
that	O
asymptotically	O
grows	O
like	O
N	O
logN	O
only	O
when	O
using	O
gap	O
sequences	O
whose	O
number	O
of	O
gaps	O
grows	O
in	O
proportion	O
to	O
the	O
logarithm	O
of	O
the	O
array	B-Data_Structure
size	O
.	O
</s>
<s>
It	O
is	O
,	O
however	O
,	O
unknown	O
whether	O
Shellsort	B-Algorithm
can	O
reach	O
this	O
asymptotic	O
order	O
of	O
average-case	O
complexity	O
,	O
which	O
is	O
optimal	O
for	O
comparison	B-Algorithm
sorts	I-Algorithm
.	O
</s>
<s>
For	O
example	O
,	O
this	O
gives	O
the	O
new	O
result	O
that	O
the	O
Janson-Knuth	O
upper	O
bound	O
is	O
matched	O
by	O
the	O
resulting	O
lower	O
bound	O
for	O
the	O
used	O
increment	O
sequence	O
,	O
showing	O
that	O
three	O
pass	O
Shellsort	B-Algorithm
for	O
this	O
increment	O
sequence	O
uses	O
comparisons/inversions/running	O
time	O
.	O
</s>
<s>
The	O
worst-case	O
complexity	O
of	O
any	O
version	O
of	O
Shellsort	B-Algorithm
is	O
of	O
higher	O
order	O
:	O
Plaxton	O
,	O
Poonen	O
,	O
and	O
Suel	O
showed	O
that	O
it	O
grows	O
at	O
least	O
as	O
rapidly	O
as	O
.	O
</s>
<s>
Shellsort	B-Algorithm
performs	O
more	O
operations	O
and	O
has	O
higher	O
cache	O
miss	O
ratio	O
than	O
quicksort	B-Algorithm
.	O
</s>
<s>
However	O
,	O
since	O
it	O
can	O
be	O
implemented	O
using	O
little	O
code	O
and	O
does	O
not	O
use	O
the	O
call	B-General_Concept
stack	I-General_Concept
,	O
some	O
implementations	O
of	O
the	O
qsort	B-Language
function	O
in	O
the	B-Language
C	I-Language
standard	I-Language
library	I-Language
targeted	O
at	O
embedded	B-Architecture
systems	I-Architecture
use	O
it	O
instead	O
of	O
quicksort	B-Algorithm
.	O
</s>
<s>
Shellsort	B-Algorithm
is	O
,	O
for	O
example	O
,	O
used	O
in	O
the	O
uClibc	B-Language
library	O
.	O
</s>
<s>
For	O
similar	O
reasons	O
,	O
in	O
the	O
past	O
,	O
Shellsort	B-Algorithm
was	O
used	O
in	O
the	O
Linux	B-Operating_System
kernel	I-Operating_System
.	O
</s>
<s>
Shellsort	B-Algorithm
can	O
also	O
serve	O
as	O
a	O
sub-algorithm	O
of	O
introspective	O
sort	O
,	O
to	O
sort	O
short	O
subarrays	O
and	O
to	O
prevent	O
a	O
slowdown	O
when	O
the	O
recursion	O
depth	O
exceeds	O
a	O
given	O
limit	O
.	O
</s>
<s>
This	O
principle	O
is	O
employed	O
,	O
for	O
instance	O
,	O
in	O
the	O
bzip2	B-Application
compressor	O
.	O
</s>
