<s>
Merge	B-Algorithm
algorithms	I-Algorithm
are	O
a	O
family	O
of	O
algorithms	O
that	O
take	O
multiple	O
sorted	B-Algorithm
lists	I-Algorithm
as	O
input	O
and	O
produce	O
a	O
single	O
list	O
as	O
output	O
,	O
containing	O
all	O
the	O
elements	O
of	O
the	O
inputs	O
lists	O
in	O
sorted	B-Algorithm
order	O
.	O
</s>
<s>
These	O
algorithms	O
are	O
used	O
as	O
subroutines	O
in	O
various	O
sorting	B-Algorithm
algorithms	I-Algorithm
,	O
most	O
famously	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
merge	B-Algorithm
algorithm	I-Algorithm
plays	O
a	O
critical	O
role	O
in	O
the	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
,	O
a	O
comparison-based	B-Algorithm
sorting	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Conceptually	O
,	O
the	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
consists	O
of	O
two	O
steps	O
:	O
</s>
<s>
Recursively	B-Algorithm
divide	O
the	O
list	O
into	O
sublists	O
of	O
(	O
roughly	O
)	O
equal	O
length	O
,	O
until	O
each	O
sublist	O
contains	O
only	O
one	O
element	O
,	O
or	O
in	O
the	O
case	O
of	O
iterative	O
(	O
bottom	O
up	O
)	O
merge	B-Algorithm
sort	I-Algorithm
,	O
consider	O
a	O
list	O
of	O
n	O
elements	O
as	O
n	O
sub-lists	O
of	O
size	O
1	O
.	O
</s>
<s>
A	O
list	O
containing	O
a	O
single	O
element	O
is	O
,	O
by	O
definition	O
,	O
sorted	B-Algorithm
.	O
</s>
<s>
Repeatedly	O
merge	B-General_Concept
sublists	O
to	O
create	O
a	O
new	O
sorted	B-Algorithm
sublist	O
until	O
the	O
single	O
list	O
contains	O
all	O
elements	O
.	O
</s>
<s>
The	O
single	O
list	O
is	O
the	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
The	O
merge	B-Algorithm
algorithm	I-Algorithm
is	O
used	O
repeatedly	O
in	O
the	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
.	O
</s>
<s>
An	O
example	O
merge	B-Algorithm
sort	I-Algorithm
is	O
given	O
in	O
the	O
illustration	O
.	O
</s>
<s>
The	O
array	O
is	O
divided	O
into	O
7	O
partitions	O
;	O
each	O
partition	O
contains	O
1	O
element	O
and	O
is	O
sorted	B-Algorithm
.	O
</s>
<s>
The	O
sorted	B-Algorithm
partitions	O
are	O
then	O
merged	O
to	O
produce	O
larger	O
,	O
sorted	B-Algorithm
,	O
partitions	O
,	O
until	O
1	O
partition	O
,	O
the	O
sorted	B-Algorithm
array	O
,	O
is	O
left	O
.	O
</s>
<s>
Merging	O
two	O
sorted	B-Algorithm
lists	I-Algorithm
into	O
one	O
can	O
be	O
done	O
in	O
linear	O
time	O
and	O
linear	O
or	O
constant	O
space	O
(	O
depending	O
on	O
the	O
data	O
access	O
model	O
)	O
.	O
</s>
<s>
The	O
following	O
pseudocode	B-Language
demonstrates	O
an	O
algorithm	O
that	O
merges	B-General_Concept
input	O
lists	O
(	O
either	O
linked	B-Data_Structure
lists	I-Data_Structure
or	O
arrays	B-Data_Structure
)	O
and	O
into	O
a	O
new	O
list	O
.	O
</s>
<s>
When	O
the	O
inputs	O
are	O
linked	B-Data_Structure
lists	I-Data_Structure
,	O
this	O
algorithm	O
can	O
be	O
implemented	O
to	O
use	O
only	O
a	O
constant	O
amount	O
of	O
working	O
space	O
;	O
the	O
pointers	O
in	O
the	O
lists	O
 '	O
nodes	O
can	O
be	O
reused	O
for	O
bookkeeping	O
and	O
for	O
constructing	O
the	O
final	O
merged	O
list	O
.	O
</s>
<s>
In	O
the	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
,	O
this	O
subroutine	O
is	O
typically	O
used	O
to	O
merge	B-General_Concept
two	O
sub-arrays	O
,	O
of	O
a	O
single	O
array	O
.	O
</s>
<s>
This	O
can	O
be	O
done	O
by	O
copying	O
the	O
sub-arrays	O
into	O
a	O
temporary	O
array	O
,	O
then	O
applying	O
the	O
merge	B-Algorithm
algorithm	I-Algorithm
above	O
.	O
</s>
<s>
Various	O
in-place	O
merge	B-Algorithm
algorithms	I-Algorithm
have	O
been	O
devised	O
,	O
sometimes	O
sacrificing	O
the	O
linear-time	O
bound	O
to	O
produce	O
an	O
algorithm	O
;	O
see	O
for	O
discussion	O
.	O
</s>
<s>
-way	O
merging	O
generalizes	O
binary	O
merging	O
to	O
an	O
arbitrary	O
number	O
of	O
sorted	B-Algorithm
input	O
lists	O
.	O
</s>
<s>
Applications	O
of	O
-way	O
merging	O
arise	O
in	O
various	O
sorting	B-Algorithm
algorithms	I-Algorithm
,	O
including	O
patience	O
sorting	B-Algorithm
and	O
an	O
external	B-Algorithm
sorting	I-Algorithm
algorithm	O
that	O
divides	O
its	O
input	O
into	O
blocks	O
that	O
fit	O
in	O
memory	O
,	O
sorts	O
these	O
one	O
by	O
one	O
,	O
then	O
merges	B-General_Concept
these	O
blocks	O
.	O
</s>
<s>
In	B-General_Concept
the	I-General_Concept
worst	I-General_Concept
case	I-General_Concept
,	O
this	O
algorithm	O
performs	O
element	O
comparisons	O
to	O
perform	O
its	O
work	O
if	O
there	O
are	O
a	O
total	O
of	O
elements	O
in	O
the	O
lists	O
.	O
</s>
<s>
It	O
can	O
be	O
improved	O
by	O
storing	O
the	O
lists	O
in	O
a	O
priority	B-Application
queue	I-Application
(	O
min-heap	B-Application
)	O
keyed	O
by	O
their	O
first	O
element	O
:	O
</s>
<s>
Build	O
a	O
min-heap	B-Application
of	O
the	O
lists	O
,	O
using	O
the	O
first	O
element	O
as	O
the	O
key	O
.	O
</s>
<s>
Re-heapify	O
.	O
</s>
<s>
Searching	O
for	O
the	O
next	O
smallest	O
element	O
to	O
be	O
output	O
(	O
find-min	O
)	O
and	O
restoring	O
heap	B-Application
order	O
can	O
now	O
be	O
done	O
in	O
time	O
(	O
more	O
specifically	O
,	O
comparisons	O
)	O
,	O
and	O
the	O
full	O
problem	O
can	O
be	O
solved	O
in	O
time	O
(	O
approximately	O
comparisons	O
)	O
.	O
</s>
<s>
A	O
third	O
algorithm	O
for	O
the	O
problem	O
is	O
a	O
divide	B-Algorithm
and	I-Algorithm
conquer	I-Algorithm
solution	O
that	O
builds	O
on	O
the	O
binary	O
merge	B-Algorithm
algorithm	I-Algorithm
:	O
</s>
<s>
If	O
,	O
perform	O
a	O
binary	O
merge	B-General_Concept
.	O
</s>
<s>
Else	O
,	O
recursively	B-Algorithm
merge	B-General_Concept
the	O
first	O
lists	O
and	O
the	O
final	O
lists	O
,	O
then	O
binary	O
merge	B-General_Concept
these	O
.	O
</s>
<s>
When	O
the	O
input	O
lists	O
to	O
this	O
algorithm	O
are	O
ordered	O
by	O
length	O
,	O
shortest	O
first	O
,	O
it	O
requires	O
fewer	O
than	O
comparisons	O
,	O
i.e.	O
,	O
less	O
than	O
half	O
the	O
number	O
used	O
by	O
the	O
heap-based	O
algorithm	O
;	O
in	O
practice	O
,	O
it	O
may	O
be	O
about	O
as	O
fast	O
or	O
slow	O
as	O
the	O
heap-based	O
algorithm	O
.	O
</s>
<s>
A	O
parallel	B-Operating_System
version	O
of	O
the	O
binary	O
merge	B-Algorithm
algorithm	I-Algorithm
can	O
serve	O
as	O
a	O
building	O
block	O
of	O
a	O
parallel	B-Operating_System
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
The	O
following	O
pseudocode	B-Language
demonstrates	O
this	O
algorithm	O
in	O
a	O
parallel	B-Operating_System
divide-and-conquer	I-Operating_System
style	O
(	O
adapted	O
from	O
Cormen	O
et	O
al	O
.	O
)	O
.	O
</s>
<s>
It	O
operates	O
on	O
two	O
sorted	B-Algorithm
arrays	B-Data_Structure
and	O
and	O
writes	O
the	O
sorted	B-Algorithm
output	O
to	O
array	O
.	O
</s>
<s>
Finally	O
,	O
each	O
pair	O
of	O
halves	O
is	O
merged	O
recursively	B-Algorithm
,	O
and	O
since	O
the	O
recursive	O
calls	O
are	O
independent	O
of	O
each	O
other	O
,	O
they	O
can	O
be	O
done	O
in	O
parallel	B-Operating_System
.	O
</s>
<s>
The	O
work	O
performed	O
by	O
the	O
algorithm	O
for	O
two	O
arrays	B-Data_Structure
holding	O
a	O
total	O
of	O
elements	O
,	O
i.e.	O
,	O
the	O
running	O
time	O
of	O
a	O
serial	O
version	O
of	O
it	O
,	O
is	O
.	O
</s>
<s>
Since	O
the	O
two	O
recursive	O
calls	O
of	O
merge	B-General_Concept
are	O
in	O
parallel	B-Operating_System
,	O
only	O
the	O
costlier	O
of	O
the	O
two	O
calls	O
needs	O
to	O
be	O
considered	O
.	O
</s>
<s>
In	B-General_Concept
the	I-General_Concept
worst	I-General_Concept
case	I-General_Concept
,	O
the	O
maximum	O
number	O
of	O
elements	O
in	O
one	O
of	O
the	O
recursive	O
calls	O
is	O
at	O
most	O
since	O
the	O
array	O
with	O
more	O
elements	O
is	O
perfectly	O
split	O
in	O
half	O
.	O
</s>
<s>
Note	O
:	O
The	O
routine	O
is	O
not	O
stable	O
:	O
if	O
equal	O
items	O
are	O
separated	O
by	O
splitting	O
and	O
,	O
they	O
will	O
become	O
interleaved	O
in	O
;	O
also	O
swapping	O
and	O
will	O
destroy	O
the	O
order	O
,	O
if	O
equal	O
items	O
are	O
spread	O
among	O
both	O
input	O
arrays	B-Data_Structure
.	O
</s>
<s>
As	O
a	O
result	O
,	O
when	O
used	O
for	O
sorting	B-Algorithm
,	O
this	O
algorithm	O
produces	O
a	O
sort	O
that	O
is	O
not	O
stable	O
.	O
</s>
<s>
There	O
are	O
also	O
algorithms	O
that	O
introduce	O
parallelism	O
within	O
a	O
single	O
instance	O
of	O
merging	O
of	O
two	O
sorted	B-Algorithm
lists	I-Algorithm
.	O
</s>
<s>
These	O
can	O
be	O
used	O
in	O
field-programmable	B-Architecture
gate	I-Architecture
arrays	I-Architecture
(	O
FPGAs	B-Architecture
)	O
,	O
specialized	O
sorting	B-Algorithm
circuits	O
,	O
as	O
well	O
as	O
in	O
modern	O
processors	O
with	O
single-instruction	O
multiple-data	O
(	O
SIMD	B-Device
)	O
instructions	O
.	O
</s>
<s>
Existing	O
parallel	B-Operating_System
algorithms	O
are	O
based	O
on	O
modifications	O
of	O
the	O
merge	B-General_Concept
part	O
of	O
either	O
the	O
bitonic	B-Algorithm
sorter	I-Algorithm
or	O
odd-even	B-Algorithm
mergesort	I-Algorithm
.	O
</s>
<s>
introduced	O
MMS	O
for	O
FPGAs	B-Architecture
,	O
which	O
focused	O
on	O
removing	O
a	O
multi-cycle	O
feedback	O
datapath	O
that	O
prevented	O
efficient	O
pipelining	O
in	O
hardware	O
.	O
</s>
<s>
introduced	O
FLiMS	O
that	O
improved	O
the	O
hardware	O
utilization	O
and	O
performance	O
by	O
only	O
requiring	O
pipeline	O
stages	O
of	O
compare-and-swap	O
units	O
to	O
merge	B-General_Concept
with	O
a	O
parallelism	O
of	O
elements	O
per	O
FPGA	B-Architecture
cycle	O
.	O
</s>
<s>
Some	O
computer	B-Language
languages	I-Language
provide	O
built-in	O
or	O
library	O
support	O
for	O
merging	O
sorted	B-Algorithm
collections	B-Application
.	O
</s>
<s>
The	O
C++'s	O
Standard	B-Application
Template	I-Application
Library	I-Application
has	O
the	O
function	O
,	O
which	O
merges	B-General_Concept
two	O
sorted	B-Algorithm
ranges	O
of	O
iterators	O
,	O
and	O
,	O
which	O
merges	B-General_Concept
two	O
consecutive	O
sorted	B-Algorithm
ranges	O
in-place	O
.	O
</s>
<s>
In	O
addition	O
,	O
the	O
(	O
linked	B-Data_Structure
list	I-Data_Structure
)	O
class	O
has	O
its	O
own	O
method	O
which	O
merges	B-General_Concept
another	O
list	O
into	O
itself	O
.	O
</s>
<s>
C++17	O
allows	O
for	O
differing	O
execution	O
policies	O
,	O
namely	O
sequential	O
,	O
parallel	B-Operating_System
,	O
and	O
parallel-unsequenced	O
.	O
</s>
<s>
Python	B-Language
's	O
standard	O
library	O
(	O
since	O
2.6	O
)	O
also	O
has	O
a	O
function	O
in	O
the	O
module	O
,	O
that	O
takes	O
multiple	O
sorted	B-Algorithm
iterables	O
,	O
and	O
merges	B-General_Concept
them	O
into	O
a	O
single	O
iterator	O
.	O
</s>
