<s>
Block	B-Algorithm
sort	I-Algorithm
,	O
or	O
block	B-Algorithm
merge	I-Algorithm
sort	I-Algorithm
,	O
is	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
combining	O
at	O
least	O
two	O
merge	B-Algorithm
operations	O
with	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
to	O
arrive	O
at	O
in-place	B-Algorithm
stable	O
sorting	B-Algorithm
.	O
</s>
<s>
It	O
gets	O
its	O
name	O
from	O
the	O
observation	O
that	O
merging	O
two	O
sorted	B-Algorithm
lists	I-Algorithm
,	O
and	O
,	O
is	O
equivalent	O
to	O
breaking	O
into	O
evenly	O
sized	O
blocks	O
,	O
inserting	O
each	O
block	O
into	O
under	O
special	O
rules	O
,	O
and	O
merging	O
pairs	O
.	O
</s>
<s>
The	O
outer	O
loop	O
of	O
block	B-Algorithm
sort	I-Algorithm
is	O
identical	O
to	O
a	O
bottom-up	O
merge	B-Algorithm
sort	O
,	O
where	O
each	O
level	O
of	O
the	O
sort	O
merges	O
pairs	O
of	O
subarrays	O
,	O
and	O
,	O
in	O
sizes	O
of	O
1	O
,	O
then	O
2	O
,	O
then	O
4	O
,	O
8	O
,	O
16	O
,	O
and	O
so	O
on	O
,	O
until	O
both	O
subarrays	O
combined	O
are	O
the	O
array	B-Data_Structure
itself	O
.	O
</s>
<s>
Rather	O
than	O
merging	O
and	O
directly	O
as	O
with	O
traditional	O
methods	O
,	O
a	O
block-based	O
merge	B-Algorithm
algorithm	I-Algorithm
divides	O
into	O
discrete	O
blocks	O
of	O
size	O
(	O
resulting	O
in	O
number	O
of	O
blocks	O
as	O
well	O
)	O
,	O
inserts	O
each	O
block	O
into	O
such	O
that	O
the	O
first	O
value	O
of	O
each	O
block	O
is	O
less	O
than	O
or	O
equal	O
( ≤	O
)	O
to	O
the	O
value	O
immediately	O
after	O
it	O
,	O
then	O
locally	O
merges	O
each	O
block	O
with	O
any	O
values	O
between	O
it	O
and	O
the	O
next	O
block	O
.	O
</s>
<s>
As	O
merges	O
still	O
require	O
a	O
separate	O
buffer	O
large	O
enough	O
to	O
hold	O
the	O
block	O
to	O
be	O
merged	O
,	O
two	O
areas	O
within	O
the	O
array	B-Data_Structure
are	O
reserved	O
for	O
this	O
purpose	O
(	O
known	O
as	O
internal	O
buffers	O
)	O
.	O
</s>
<s>
Once	O
every	O
and	O
block	O
of	O
every	O
and	O
subarray	O
have	O
been	O
merged	O
for	O
that	O
level	O
of	O
the	O
merge	B-Algorithm
sort	O
,	O
the	O
values	O
in	O
that	O
buffer	O
must	O
be	O
sorted	O
to	O
restore	O
their	O
original	O
order	O
,	O
so	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
must	O
be	O
applied	O
.	O
</s>
<s>
The	O
values	O
in	O
the	O
buffers	O
are	O
then	O
redistributed	O
to	O
their	O
first	O
sorted	O
position	O
within	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
This	O
process	O
repeats	O
for	O
each	O
level	O
of	O
the	O
outer	O
bottom-up	O
merge	B-Algorithm
sort	O
,	O
at	O
which	O
point	O
the	O
array	B-Data_Structure
will	O
have	O
been	O
stably	O
sorted	O
.	O
</s>
<s>
Additionally	O
,	O
block	B-Algorithm
sort	I-Algorithm
relies	O
on	O
the	O
following	O
operations	O
as	O
part	O
of	O
its	O
overall	O
algorithm	O
:	O
</s>
<s>
Swap	O
:	O
exchange	O
the	O
positions	O
of	O
two	O
values	O
in	O
an	O
array	B-Data_Structure
.	O
</s>
<s>
Block	B-Algorithm
swap	I-Algorithm
:	O
exchange	O
a	O
range	O
of	O
values	O
within	O
an	O
array	B-Data_Structure
with	O
values	O
in	O
a	O
different	O
range	O
of	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Binary	O
search	O
:	O
assuming	O
the	O
array	B-Data_Structure
is	O
sorted	O
,	O
check	O
the	O
middle	O
value	O
of	O
the	O
current	O
search	O
range	O
,	O
then	O
if	O
the	O
value	O
is	O
lesser	O
check	O
the	O
lower	O
range	O
,	O
and	O
if	O
the	O
value	O
is	O
greater	O
check	O
the	O
upper	O
range	O
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
uses	O
two	O
variants	O
:	O
one	O
which	O
finds	O
the	O
first	O
position	O
to	O
insert	O
a	O
value	O
in	O
the	O
sorted	O
array	B-Data_Structure
,	O
and	O
one	O
which	O
finds	O
the	O
last	O
position	O
.	O
</s>
<s>
Linear	B-Algorithm
search	I-Algorithm
:	O
find	O
a	O
particular	O
value	O
in	O
an	O
array	B-Data_Structure
by	O
checking	O
every	O
single	O
element	O
in	O
order	O
,	O
until	O
it	O
is	O
found	O
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
:	O
for	O
each	O
item	O
in	O
the	O
array	B-Data_Structure
,	O
loop	O
backward	O
and	O
find	O
where	O
it	O
needs	O
to	O
be	O
inserted	O
,	O
then	O
insert	O
it	O
at	O
that	O
position	O
.	O
</s>
<s>
Array	B-Data_Structure
rotation	O
:	O
move	O
the	O
items	O
in	O
an	O
array	B-Data_Structure
to	O
the	O
left	O
or	O
right	O
by	O
some	O
number	O
of	O
spaces	O
,	O
with	O
values	O
on	O
the	O
edges	O
wrapping	O
around	O
to	O
the	O
other	O
side	O
.	O
</s>
<s>
As	O
previously	O
stated	O
,	O
the	O
outer	O
loop	O
of	O
a	O
block	B-Algorithm
sort	I-Algorithm
is	O
identical	O
to	O
a	O
bottom-up	O
merge	B-Algorithm
sort	O
.	O
</s>
<s>
The	O
two	O
internal	O
buffers	O
needed	O
for	O
each	O
level	O
of	O
the	O
merge	B-Algorithm
step	O
are	O
created	O
by	O
moving	O
the	O
first	O
2	O
first	O
instances	O
of	O
their	O
values	O
within	O
an	O
subarray	O
to	O
the	O
start	O
of	O
.	O
</s>
<s>
First	O
it	O
iterates	O
over	O
the	O
elements	O
in	O
and	O
counts	O
off	O
the	O
unique	O
values	O
it	O
needs	O
,	O
then	O
it	O
applies	O
array	B-Data_Structure
rotations	O
to	O
move	O
those	O
unique	O
values	O
to	O
the	O
start	O
.	O
</s>
<s>
Once	O
the	O
one	O
or	O
two	O
internal	O
buffers	O
have	O
been	O
created	O
,	O
it	O
begins	O
merging	O
each	O
and	O
subarray	O
for	O
this	O
level	O
of	O
the	O
merge	B-Algorithm
sort	O
.	O
</s>
<s>
//	O
locally	O
merge	B-Algorithm
the	O
previous	O
A	O
block	O
with	O
the	O
B	O
values	O
that	O
follow	O
it	O
,	O
</s>
<s>
So	O
rather	O
than	O
rotating	O
the	O
second	O
buffer	O
(	O
which	O
used	O
to	O
be	O
the	O
A	O
block	O
before	O
the	O
block	B-Algorithm
swap	I-Algorithm
)	O
into	O
the	O
previous	O
B	O
block	O
at	O
position	O
index	O
,	O
the	O
values	O
in	O
the	O
B	O
block	O
after	O
index	O
can	O
simply	O
be	O
block	O
swapped	O
with	O
the	O
last	O
items	O
of	O
the	O
buffer	O
.	O
</s>
<s>
The	O
floating	O
hole	O
in	O
this	O
case	O
refers	O
to	O
the	O
contents	O
of	O
the	O
second	O
internal	O
buffer	O
floating	O
around	O
the	O
array	B-Data_Structure
,	O
and	O
acting	O
as	O
a	O
hole	O
in	O
the	O
sense	O
that	O
the	O
items	O
do	O
not	O
need	O
to	O
retain	O
their	O
order	O
.	O
</s>
<s>
If	O
the	O
second	O
buffer	O
does	O
not	O
exist	O
,	O
a	O
strictly	O
in-place	B-Algorithm
merge	B-Algorithm
operation	O
must	O
be	O
performed	O
,	O
such	O
as	O
a	O
rotation-based	O
version	O
of	O
the	O
Hwang	O
and	O
Lin	O
algorithm	O
,	O
the	O
Dudzinski	O
and	O
Dydek	O
algorithm	O
,	O
or	O
a	O
repeated	O
binary	O
search	O
and	O
rotate	O
.	O
</s>
<s>
After	O
dropping	O
the	O
minimum	O
A	O
block	O
and	O
merging	O
the	O
previous	O
A	O
block	O
with	O
the	O
B	O
values	O
that	O
follow	O
it	O
,	O
the	O
new	O
minimum	O
A	O
block	O
must	O
be	O
found	O
within	O
the	O
blocks	O
that	O
are	O
still	O
being	O
rolled	O
through	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
This	O
is	O
handled	O
by	O
running	O
a	O
linear	B-Algorithm
search	I-Algorithm
through	O
those	O
A	O
blocks	O
and	O
comparing	O
the	O
tag	O
values	O
to	O
find	O
the	O
smallest	O
one	O
.	O
</s>
<s>
These	O
remaining	O
A	O
blocks	O
then	O
continue	O
rolling	O
through	O
the	O
array	B-Data_Structure
and	O
being	O
dropped	O
and	O
inserted	O
where	O
they	O
belong	O
.	O
</s>
<s>
This	O
completes	O
the	O
merge	B-Algorithm
process	O
for	O
that	O
particular	O
pair	O
of	O
A	O
and	O
B	O
subarrays	O
.	O
</s>
<s>
However	O
,	O
it	O
must	O
then	O
repeat	O
the	O
process	O
for	O
the	O
remaining	O
A	O
and	O
B	O
subarrays	O
for	O
the	O
current	O
level	O
of	O
the	O
merge	B-Algorithm
sort	O
.	O
</s>
<s>
Note	O
that	O
the	O
internal	O
buffers	O
can	O
be	O
reused	O
for	O
every	O
set	O
of	O
A	O
and	O
B	O
subarrays	O
for	O
this	O
level	O
of	O
the	O
merge	B-Algorithm
sort	O
,	O
and	O
do	O
not	O
need	O
to	O
be	O
re-extracted	O
or	O
modified	O
in	O
any	O
way	O
.	O
</s>
<s>
This	O
means	O
the	O
contents	O
of	O
the	O
second	O
buffer	O
will	O
need	O
to	O
be	O
sorted	O
using	O
a	O
different	O
algorithm	O
,	O
such	O
as	O
insertion	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
two	O
buffers	O
must	O
then	O
be	O
redistributed	O
back	O
into	O
the	O
array	B-Data_Structure
using	O
the	O
opposite	O
process	O
that	O
was	O
used	O
to	O
create	O
them	O
.	O
</s>
<s>
After	O
repeating	O
these	O
steps	O
for	O
every	O
level	O
of	O
the	O
bottom-up	O
merge	B-Algorithm
sort	O
,	O
the	O
block	B-Algorithm
sort	I-Algorithm
is	O
completed	O
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
works	O
by	O
extracting	O
two	O
internal	O
buffers	O
,	O
breaking	O
A	O
and	O
B	O
subarrays	O
into	O
evenly	O
sized	O
blocks	O
,	O
rolling	O
and	O
dropping	O
the	O
A	O
blocks	O
into	O
B	O
(	O
using	O
the	O
first	O
buffer	O
to	O
track	O
the	O
order	O
of	O
the	O
A	O
blocks	O
)	O
,	O
locally	O
merging	O
using	O
the	O
second	O
buffer	O
as	O
swap	O
space	O
,	O
sorting	B-Algorithm
the	O
second	O
buffer	O
,	O
and	O
redistributing	O
both	O
buffers	O
.	O
</s>
<s>
One	O
variant	O
of	O
block	B-Algorithm
sort	I-Algorithm
allows	O
it	O
to	O
use	O
any	O
amount	O
of	O
additional	O
memory	O
provided	O
to	O
it	O
,	O
by	O
using	O
this	O
external	O
buffer	O
for	O
merging	O
an	O
A	O
subarray	O
or	O
A	O
block	O
with	O
B	O
whenever	O
A	O
fits	O
into	O
it	O
.	O
</s>
<s>
In	O
this	O
situation	O
it	O
would	O
be	O
identical	O
to	O
a	O
merge	B-Algorithm
sort	O
.	O
</s>
<s>
Once	O
all	O
of	O
the	O
A	O
blocks	O
have	O
been	O
dropped	O
,	O
the	O
movement-imitation	O
buffer	O
is	O
used	O
to	O
decode	O
whether	O
a	O
given	O
block	O
in	O
the	O
array	B-Data_Structure
is	O
an	O
A	O
block	O
or	O
a	O
B	O
block	O
,	O
each	O
A	O
block	O
is	O
rotated	O
into	O
B	O
,	O
and	O
the	O
second	O
internal	O
buffer	O
is	O
used	O
as	O
swap	O
space	O
for	O
the	O
local	O
merges	O
.	O
</s>
<s>
Many	O
sorting	B-Algorithm
algorithms	I-Algorithm
can	O
be	O
used	O
to	O
sort	O
the	O
contents	O
of	O
the	O
second	O
internal	O
buffer	O
,	O
including	O
unstable	O
sorts	O
like	O
quicksort	B-Algorithm
,	O
since	O
the	O
contents	O
of	O
the	O
buffer	O
are	O
guaranteed	O
to	O
unique	O
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
still	O
recommended	O
,	O
though	O
,	O
for	O
its	O
situational	O
performance	O
and	O
lack	O
of	O
recursion	O
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
is	O
a	O
well-defined	O
and	O
testable	O
class	O
of	O
algorithms	O
,	O
with	O
working	O
implementations	O
available	O
as	O
a	O
merge	B-Algorithm
and	O
as	O
a	O
sort	O
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
begins	O
by	O
performing	O
insertion	B-Algorithm
sort	I-Algorithm
on	O
groups	O
of	O
1631	O
items	O
in	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
Insertion	B-Algorithm
sort	I-Algorithm
is	O
an	O
operation	O
,	O
so	O
this	O
leads	O
to	O
anywhere	O
from	O
to	O
,	O
which	O
is	O
once	O
the	O
constant	O
factors	O
are	O
omitted	O
.	O
</s>
<s>
It	O
must	O
also	O
apply	O
an	O
insertion	B-Algorithm
sort	I-Algorithm
on	O
the	O
second	O
internal	O
buffer	O
after	O
each	O
level	O
of	O
merging	O
is	O
completed	O
.	O
</s>
<s>
Next	O
it	O
must	O
extract	O
two	O
internal	O
buffers	O
for	O
each	O
level	O
of	O
the	O
merge	B-Algorithm
sort	O
.	O
</s>
<s>
In	O
the	O
worst	O
case	O
this	O
will	O
end	O
up	O
searching	O
the	O
entire	O
array	B-Data_Structure
before	O
finding	O
non-contiguous	O
unique	O
values	O
,	O
which	O
requires	O
comparisons	O
and	O
rotations	O
for	O
values	O
.	O
</s>
<s>
When	O
none	O
of	O
the	O
A	O
or	O
B	O
subarrays	O
contained	O
unique	O
values	O
to	O
create	O
the	O
internal	O
buffers	O
,	O
a	O
normally	O
suboptimal	O
in-place	B-Algorithm
merge	B-Algorithm
operation	O
is	O
performed	O
where	O
it	O
repeatedly	O
binary	O
searches	O
and	O
rotates	O
A	O
into	O
B	O
.	O
</s>
<s>
The	O
local	O
merges	O
retain	O
the	O
same	O
complexity	O
of	O
a	O
standard	O
merge	B-Algorithm
,	O
albeit	O
with	O
more	O
assignments	O
since	O
the	O
values	O
must	O
be	O
swapped	O
rather	O
than	O
copied	O
.	O
</s>
<s>
The	O
linear	B-Algorithm
search	I-Algorithm
for	O
finding	O
the	O
new	O
minimum	O
A	O
block	O
iterates	O
over	O
blocks	O
times	O
.	O
</s>
<s>
After	O
omitting	O
all	O
but	O
the	O
highest	O
complexity	O
and	O
considering	O
that	O
there	O
are	O
levels	O
in	O
the	O
outer	O
merge	B-Algorithm
loop	O
,	O
this	O
leads	O
to	O
a	O
final	O
asymptotic	O
complexity	O
of	O
for	O
the	O
worst	O
and	O
average	O
cases	O
.	O
</s>
<s>
For	O
the	O
best	O
case	O
,	O
where	O
the	O
data	O
is	O
already	O
in	O
order	O
,	O
the	O
merge	B-Algorithm
step	O
performs	O
comparisons	O
for	O
the	O
first	O
level	O
,	O
then	O
,	O
,	O
,	O
etc	O
.	O
</s>
<s>
As	O
block	B-Algorithm
sort	I-Algorithm
is	O
non-recursive	O
and	O
does	O
not	O
require	O
the	O
use	O
of	O
dynamic	O
allocations	O
,	O
this	O
leads	O
to	O
constant	O
stack	B-Application
and	O
heap	O
space	O
.	O
</s>
<s>
It	O
uses	O
O(1 )	O
auxiliary	O
memory	O
in	O
a	O
transdichotomous	O
model	O
,	O
which	O
accepts	O
that	O
the	O
O(log n )	O
bits	O
needed	O
to	O
keep	O
track	O
of	O
the	O
ranges	O
for	O
A	O
and	O
B	O
cannot	O
be	O
any	O
greater	O
than	O
32	O
or	O
64	O
on	O
32-bit	O
or	O
64-bit	B-Device
computing	I-Device
systems	O
,	O
respectively	O
,	O
and	O
therefore	O
simplifies	O
to	O
O(1 )	O
space	O
for	O
any	O
array	B-Data_Structure
that	O
can	O
feasibly	O
be	O
allocated	O
.	O
</s>
<s>
Although	O
items	O
in	O
the	O
array	B-Data_Structure
are	O
moved	O
out	O
of	O
order	O
during	O
a	O
block	B-Algorithm
sort	I-Algorithm
,	O
each	O
operation	O
is	O
fully	O
reversible	O
and	O
will	O
have	O
restored	O
the	O
original	O
order	O
of	O
equivalent	O
items	O
by	O
its	O
completion	O
.	O
</s>
<s>
Stability	O
requires	O
the	O
first	O
instance	O
of	O
each	O
value	O
in	O
an	O
array	B-Data_Structure
before	O
sorting	B-Algorithm
to	O
still	O
be	O
the	O
first	O
instance	O
of	O
that	O
value	O
after	O
sorting	B-Algorithm
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
moves	O
these	O
first	O
instances	O
to	O
the	O
start	O
of	O
the	O
array	B-Data_Structure
to	O
create	O
the	O
two	O
internal	O
buffers	O
,	O
but	O
when	O
all	O
of	O
the	O
merges	O
are	O
completed	O
for	O
the	O
current	O
level	O
of	O
the	O
block	B-Algorithm
sort	I-Algorithm
,	O
those	O
values	O
are	O
distributed	O
back	O
to	O
the	O
first	O
sorted	O
position	O
within	O
the	O
array	B-Data_Structure
.	O
</s>
<s>
However	O
,	O
as	O
the	O
algorithm	O
already	O
ensured	O
the	O
buffer	O
only	O
contains	O
unique	O
values	O
,	O
sorting	B-Algorithm
the	O
contents	O
of	O
the	O
buffer	O
is	O
sufficient	O
to	O
restore	O
their	O
original	O
stable	O
order	O
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
is	O
an	O
adaptive	B-Algorithm
sort	I-Algorithm
on	O
two	O
levels	O
:	O
first	O
,	O
it	O
skips	O
merging	O
A	O
and	O
B	O
subarrays	O
that	O
are	O
already	O
in	O
order	O
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
is	O
a	O
stable	O
sort	O
that	O
does	O
not	O
require	O
additional	O
memory	O
,	O
which	O
is	O
useful	O
in	O
cases	O
where	O
there	O
is	O
not	O
enough	O
free	O
memory	O
to	O
allocate	O
the	O
O(n )	O
buffer	O
.	O
</s>
<s>
When	O
using	O
the	O
external	O
buffer	O
variant	O
of	O
block	B-Algorithm
sort	I-Algorithm
,	O
it	O
can	O
scale	O
from	O
using	O
O(n )	O
memory	O
to	O
progressively	O
smaller	O
buffers	O
as	O
needed	O
,	O
and	O
will	O
still	O
work	O
efficiently	O
within	O
those	O
constraints	O
.	O
</s>
<s>
Block	B-Algorithm
sort	I-Algorithm
does	O
not	O
exploit	O
sorted	O
ranges	O
of	O
data	O
on	O
as	O
fine	O
a	O
level	O
as	O
some	O
other	O
algorithms	O
,	O
such	O
as	O
Timsort	B-Algorithm
.	O
</s>
<s>
It	O
is	O
also	O
harder	O
to	O
implement	O
and	O
parallelize	O
compared	O
to	O
a	O
merge	B-Algorithm
sort	O
.	O
</s>
