<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
k-way	B-Algorithm
merge	I-Algorithm
algorithms	I-Algorithm
or	O
multiway	O
merges	O
are	O
a	O
specific	O
type	O
of	O
sequence	B-Algorithm
merge	I-Algorithm
algorithms	I-Algorithm
that	O
specialize	O
in	O
taking	O
in	O
k	O
sorted	O
lists	O
and	O
merging	O
them	O
into	O
a	O
single	O
sorted	O
list	O
.	O
</s>
<s>
These	O
merge	B-Algorithm
algorithms	I-Algorithm
generally	O
refer	O
to	O
merge	B-Algorithm
algorithms	I-Algorithm
that	O
take	O
in	O
a	O
number	O
of	O
sorted	O
lists	O
greater	O
than	O
two	O
.	O
</s>
<s>
Two-way	O
merges	O
are	O
also	O
referred	O
to	O
as	O
binary	O
merges.The	O
k	O
-	O
way	O
merge	O
also	O
external	B-Algorithm
sorting	I-Algorithm
algorithm	O
.	O
</s>
<s>
A	O
2-way	O
merge	O
,	O
or	O
a	O
binary	O
merge	O
,	O
has	O
been	O
studied	O
extensively	O
due	O
to	O
its	O
key	O
role	O
in	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
An	O
example	O
of	O
such	O
is	O
the	O
classic	O
merge	O
that	O
appears	O
frequently	O
in	O
merge	B-Algorithm
sort	I-Algorithm
examples	O
.	O
</s>
<s>
The	O
canonical	O
2-way	O
merge	B-Algorithm
algorithm	I-Algorithm
stores	O
indices	O
i	O
,	O
j	O
,	O
and	O
k	O
into	O
A	O
,	O
B	O
,	O
and	O
C	O
respectively	O
.	O
</s>
<s>
By	O
using	O
either	O
heaps	B-Application
,	O
tournament	O
trees	O
,	O
or	O
splay	O
trees	O
,	O
the	O
smallest	O
element	O
can	O
be	O
determined	O
in	O
O(log k )	O
time	O
.	O
</s>
<s>
The	O
heap	B-Application
is	O
more	O
commonly	O
used	O
,	O
although	O
a	O
tournament	O
tree	O
is	O
faster	O
in	O
practice	O
.	O
</s>
<s>
A	O
heap	B-Application
uses	O
approximately	O
2*	O
log(k )	O
comparisons	O
in	O
each	O
step	O
because	O
it	O
handles	O
the	O
tree	O
from	O
the	O
root	O
down	O
to	O
the	O
bottom	O
and	O
needs	O
to	O
compare	O
both	O
children	O
of	O
each	O
node	O
.	O
</s>
<s>
The	O
idea	O
is	O
to	O
maintain	O
a	O
min-heap	B-Application
of	O
the	O
k	O
lists	O
,	O
each	O
keyed	O
by	O
their	O
smallest	O
current	O
element	O
.	O
</s>
<s>
A	O
simple	O
algorithm	O
builds	O
an	O
output	O
buffer	O
with	O
nodes	O
from	O
the	O
heap	B-Application
.	O
</s>
<s>
Start	O
by	O
building	O
a	O
min-heap	B-Application
of	O
nodes	O
,	O
where	O
each	O
node	O
consists	O
of	O
a	O
head	O
element	O
of	O
the	O
list	O
,	O
and	O
the	O
rest	O
(	O
or	O
tail	O
)	O
of	O
the	O
list	O
.	O
</s>
<s>
Because	O
the	O
lists	O
are	O
sorted	O
initially	O
,	O
the	O
head	O
is	O
the	O
smallest	O
element	O
of	O
each	O
list	O
;	O
the	O
heap	B-Application
property	I-Application
guarantees	O
that	O
the	O
root	O
contains	O
the	O
minimum	O
element	O
over	O
all	O
lists	O
.	O
</s>
<s>
Extract	O
the	O
root	O
node	O
from	O
the	O
heap	B-Application
,	O
add	O
the	O
head	O
element	O
to	O
the	O
output	O
buffer	O
,	O
create	O
a	O
new	O
node	O
out	O
of	O
the	O
tail	O
,	O
and	O
insert	O
it	O
into	O
the	O
heap	B-Application
.	O
</s>
<s>
Repeat	O
until	O
there	O
is	O
only	O
one	O
node	O
left	O
in	O
the	O
heap	B-Application
,	O
at	O
which	O
point	O
just	O
append	O
that	O
remaining	O
list	O
(	O
head	O
and	O
tail	O
)	O
to	O
the	O
output	O
buffer	O
.	O
</s>
<s>
allocates	O
a	O
min-heap	B-Application
of	O
pointers	O
into	O
the	O
input	O
arrays	O
.	O
</s>
<s>
In	O
an	O
O(k )	O
preprocessing	O
step	O
the	O
heap	B-Application
is	O
created	O
using	O
the	O
standard	O
heapify	B-Application
procedure	O
.	O
</s>
<s>
In	O
the	O
following	O
pseudocode	B-Language
,	O
an	O
object	O
oriented	O
tree	O
is	O
used	O
instead	O
of	O
an	O
array	O
because	O
it	O
is	O
easier	O
to	O
understand	O
.	O
</s>
<s>
One	O
can	O
show	O
that	O
no	O
comparison-based	O
k-way	B-Algorithm
merge	I-Algorithm
algorithm	I-Algorithm
exists	O
with	O
a	O
running	O
time	O
in	O
O( n	O
f(k )	O
)	O
where	O
f	O
grows	O
asymptotically	O
slower	O
than	O
a	O
logarithm	O
,	O
and	O
n	O
being	O
the	O
total	O
number	O
of	O
elements	O
.	O
</s>
<s>
Merge	O
these	O
n	O
arrays	O
with	O
the	O
k-way	B-Algorithm
merge	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
k-way	O
merges	O
are	O
used	O
in	O
external	B-Algorithm
sorting	I-Algorithm
procedures	O
.	O
</s>
<s>
External	B-Algorithm
sorting	I-Algorithm
algorithms	I-Algorithm
are	O
a	O
class	O
of	O
sorting	O
algorithms	O
that	O
can	O
handle	O
massive	O
amounts	O
of	O
data	O
.	O
</s>
<s>
External	B-Algorithm
sorting	I-Algorithm
is	O
required	O
when	O
the	O
data	O
being	O
sorted	O
do	O
not	O
fit	O
into	O
the	O
main	O
memory	O
of	O
a	O
computing	O
device	O
(	O
usually	O
RAM	O
)	O
and	O
instead	O
they	O
must	O
reside	O
in	O
the	O
slower	O
external	O
memory	O
(	O
usually	O
a	O
hard	O
drive	O
)	O
.	O
</s>
<s>
k-way	B-Algorithm
merge	I-Algorithm
algorithms	I-Algorithm
usually	O
take	O
place	O
in	O
the	O
second	O
stage	O
of	O
external	B-Algorithm
sorting	I-Algorithm
algorithms	I-Algorithm
,	O
much	O
like	O
they	O
do	O
for	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
