<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
merge	B-Algorithm
sort	I-Algorithm
(	O
also	O
commonly	O
spelled	O
as	O
mergesort	B-Algorithm
)	O
is	O
an	O
efficient	O
,	O
general-purpose	O
,	O
and	O
comparison-based	B-Algorithm
sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
is	O
a	O
divide-and-conquer	B-Algorithm
algorithm	I-Algorithm
that	O
was	O
invented	O
by	O
John	O
von	O
Neumann	O
in	O
1945	O
.	O
</s>
<s>
A	O
detailed	O
description	O
and	O
analysis	O
of	O
bottom-up	O
merge	B-Algorithm
sort	I-Algorithm
appeared	O
in	O
a	O
report	O
by	O
Goldstine	O
and	O
von	O
Neumann	O
as	O
early	O
as	O
1948	O
.	O
</s>
<s>
Conceptually	O
,	O
a	O
merge	B-Algorithm
sort	I-Algorithm
works	O
as	O
follows	O
:	O
</s>
<s>
Divide	O
the	O
unsorted	B-Algorithm
list	I-Algorithm
into	O
n	O
sublists	O
,	O
each	O
containing	O
one	O
element	O
(	O
a	O
list	O
of	O
one	O
element	O
is	O
considered	O
sorted	O
)	O
.	O
</s>
<s>
Repeatedly	O
merge	B-Algorithm
sublists	O
to	O
produce	O
new	O
sorted	O
sublists	O
until	O
there	O
is	O
only	O
one	O
sublist	O
remaining	O
.	O
</s>
<s>
This	O
will	O
be	O
the	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
Example	O
C-like	B-Language
code	O
using	O
indices	O
for	O
top-down	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
that	O
recursively	O
splits	O
the	O
list	O
(	O
called	O
runs	B-Algorithm
in	O
this	O
example	O
)	O
into	O
sublists	O
until	O
sublist	O
size	O
is	O
1	O
,	O
then	O
merges	O
those	O
sublists	O
to	O
produce	O
a	O
sorted	B-Algorithm
list	I-Algorithm
.	O
</s>
<s>
The	O
copy	O
back	O
step	O
is	O
avoided	O
with	O
alternating	O
the	O
direction	O
of	O
the	O
merge	B-Algorithm
with	O
each	O
level	O
of	O
recursion	O
(	O
except	O
for	O
an	O
initial	O
one-time	O
copy	O
,	O
that	O
can	O
be	O
avoided	O
too	O
)	O
.	O
</s>
<s>
To	O
help	O
understand	O
this	O
,	O
consider	O
an	O
array	B-Data_Structure
with	O
two	O
elements	O
.	O
</s>
<s>
If	O
there	O
are	O
four	O
elements	O
,	O
when	O
the	O
bottom	O
of	O
the	O
recursion	O
level	O
is	O
reached	O
,	O
single	O
element	O
runs	B-Algorithm
from	O
A[]	O
are	O
merged	O
to	O
B[],	O
and	O
then	O
at	O
the	O
next	O
higher	O
level	O
of	O
recursion	O
,	O
those	O
two-element	O
runs	B-Algorithm
are	O
merged	O
to	O
A[]	O
.	O
</s>
<s>
Sorting	B-Algorithm
the	O
entire	O
array	B-Data_Structure
is	O
accomplished	O
by	O
.	O
</s>
<s>
Example	O
C-like	B-Language
code	O
using	O
indices	O
for	O
bottom-up	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
which	O
treats	O
the	O
list	O
as	O
an	O
array	B-Data_Structure
of	O
n	O
sublists	O
(	O
called	O
runs	B-Algorithm
in	O
this	O
example	O
)	O
of	O
size	O
1	O
,	O
and	O
iteratively	O
merges	O
sub-lists	O
back	O
and	O
forth	O
between	O
two	O
buffers	O
:	O
</s>
<s>
Pseudocode	B-Language
for	O
top-down	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
which	O
recursively	O
divides	O
the	O
input	O
list	O
into	O
smaller	O
sublists	O
until	O
the	O
sublists	O
are	O
trivially	O
sorted	O
,	O
and	O
then	O
merges	O
the	O
sublists	O
while	O
returning	O
up	O
the	O
call	O
chain	O
.	O
</s>
<s>
//	O
Then	O
merge	B-Algorithm
the	O
now-sorted	O
sublists	O
.	O
</s>
<s>
Pseudocode	B-Language
for	O
bottom-up	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
which	O
uses	O
a	O
small	O
fixed	O
size	O
array	B-Data_Structure
of	O
references	O
to	O
nodes	O
,	O
where	O
array[i]	O
is	O
either	O
a	O
reference	O
to	O
a	O
list	O
of	O
size	O
2i	O
or	O
nil	O
.	O
</s>
<s>
The	O
merge( )	O
function	O
would	O
be	O
similar	O
to	O
the	O
one	O
shown	O
in	O
the	O
top-down	O
merge	B-Algorithm
lists	O
example	O
,	O
it	O
merges	O
two	O
already	O
sorted	B-Algorithm
lists	I-Algorithm
,	O
and	O
handles	O
empty	O
lists	O
.	O
</s>
<s>
In	O
this	O
case	O
,	O
merge( )	O
would	O
use	O
node	O
for	O
its	O
input	O
parameters	O
and	O
return	O
value	O
.	O
</s>
<s>
In	O
sorting	B-Algorithm
n	O
objects	O
,	O
merge	B-Algorithm
sort	I-Algorithm
has	O
an	O
average	B-General_Concept
and	O
worst-case	B-General_Concept
performance	I-General_Concept
of	O
O(nlogn )	O
.	O
</s>
<s>
If	O
the	O
running	O
time	O
of	O
merge	B-Algorithm
sort	I-Algorithm
for	O
a	O
list	O
of	O
length	O
n	O
is	O
T(n )	O
,	O
then	O
the	O
recurrence	O
relation	O
T(n )	O
=	O
2T( 	O
n/2	O
)	O
+	O
n	O
follows	O
from	O
the	O
definition	O
of	O
the	O
algorithm	O
(	O
apply	O
the	O
algorithm	O
to	O
two	O
lists	O
of	O
half	O
the	O
size	O
of	O
the	O
original	O
list	O
,	O
and	O
add	O
the	O
n	O
steps	O
taken	O
to	O
merge	B-Algorithm
the	O
resulting	O
two	O
lists	O
)	O
.	O
</s>
<s>
The	O
closed	O
form	O
follows	O
from	O
the	O
master	B-Algorithm
theorem	I-Algorithm
for	I-Algorithm
divide-and-conquer	I-Algorithm
recurrences	I-Algorithm
.	O
</s>
<s>
The	O
number	O
of	O
comparisons	O
made	O
by	O
merge	B-Algorithm
sort	I-Algorithm
in	O
the	O
worst	B-General_Concept
case	I-General_Concept
is	O
given	O
by	O
the	O
sorting	B-Algorithm
numbers	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
's	O
best	B-General_Concept
case	I-General_Concept
takes	O
about	O
half	O
as	O
many	O
iterations	O
as	O
its	O
worst	B-General_Concept
case	I-General_Concept
.	O
</s>
<s>
In	O
the	O
worst	B-General_Concept
case	I-General_Concept
,	O
merge	B-Algorithm
sort	I-Algorithm
uses	O
approximately	O
39%	O
fewer	O
comparisons	O
than	O
quicksort	B-Algorithm
does	O
in	O
its	O
average	B-General_Concept
case	I-General_Concept
,	O
and	O
in	O
terms	O
of	O
moves	O
,	O
merge	B-Algorithm
sort	I-Algorithm
's	O
worst	B-General_Concept
case	I-General_Concept
complexity	O
is	O
O(nlogn )	O
-	O
the	O
same	O
complexity	O
as	O
quicksort	B-Algorithm
's	O
best	B-General_Concept
case	I-General_Concept
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
is	O
more	O
efficient	O
than	O
quicksort	B-Algorithm
for	O
some	O
types	O
of	O
lists	O
if	O
the	O
data	O
to	O
be	O
sorted	O
can	O
only	O
be	O
efficiently	O
accessed	O
sequentially	O
,	O
and	O
is	O
thus	O
popular	O
in	O
languages	O
such	O
as	O
Lisp	B-Language
,	O
where	O
sequentially	O
accessed	O
data	O
structures	O
are	O
very	O
common	O
.	O
</s>
<s>
Unlike	O
some	O
(	O
efficient	O
)	O
implementations	O
of	O
quicksort	B-Algorithm
,	O
merge	B-Algorithm
sort	I-Algorithm
is	O
a	O
stable	O
sort	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
's	O
most	O
common	O
implementation	O
does	O
not	O
sort	B-Algorithm
in	I-Algorithm
place	I-Algorithm
;	O
therefore	O
,	O
the	O
memory	O
size	O
of	O
the	O
input	O
must	O
be	O
allocated	O
for	O
the	O
sorted	O
output	O
to	O
be	O
stored	O
in	O
(	O
see	O
below	O
for	O
variations	O
that	O
need	O
only	O
n/2	O
extra	O
spaces	O
)	O
.	O
</s>
<s>
A	O
natural	O
merge	B-Algorithm
sort	I-Algorithm
is	O
similar	O
to	O
a	O
bottom-up	O
merge	B-Algorithm
sort	I-Algorithm
except	O
that	O
any	O
naturally	O
occurring	O
runs	B-Algorithm
(	O
sorted	O
sequences	O
)	O
in	O
the	O
input	O
are	O
exploited	O
.	O
</s>
<s>
Both	O
monotonic	O
and	O
bitonic	O
(	O
alternating	O
up/down	O
)	O
runs	B-Algorithm
may	O
be	O
exploited	O
,	O
with	O
lists	O
(	O
or	O
equivalently	O
tapes	O
or	O
files	O
)	O
being	O
convenient	O
data	O
structures	O
(	O
used	O
as	O
FIFO	B-Application
queues	I-Application
or	O
LIFO	B-Application
stacks	I-Application
)	O
.	O
</s>
<s>
In	O
the	O
bottom-up	O
merge	B-Algorithm
sort	I-Algorithm
,	O
the	O
starting	O
point	O
assumes	O
each	O
run	O
is	O
one	O
item	O
long	O
.	O
</s>
<s>
In	O
practice	O
,	O
random	O
input	O
data	O
will	O
have	O
many	O
short	O
runs	B-Algorithm
that	O
just	O
happen	O
to	O
be	O
sorted	O
.	O
</s>
<s>
In	O
the	O
typical	O
case	O
,	O
the	O
natural	O
merge	B-Algorithm
sort	I-Algorithm
may	O
not	O
need	O
as	O
many	O
passes	O
because	O
there	O
are	O
fewer	O
runs	B-Algorithm
to	O
merge	B-Algorithm
.	O
</s>
<s>
In	O
the	O
best	B-General_Concept
case	I-General_Concept
,	O
the	O
input	O
is	O
already	O
sorted	O
(	O
i.e.	O
,	O
is	O
one	O
run	O
)	O
,	O
so	O
the	O
natural	O
merge	B-Algorithm
sort	I-Algorithm
need	O
only	O
make	O
one	O
pass	O
through	O
the	O
data	O
.	O
</s>
<s>
In	O
many	O
practical	O
cases	O
,	O
long	O
natural	O
runs	B-Algorithm
are	O
present	O
,	O
and	O
for	O
that	O
reason	O
natural	O
merge	B-Algorithm
sort	I-Algorithm
is	O
exploited	O
as	O
the	O
key	O
component	O
of	O
Timsort	B-Algorithm
.	O
</s>
<s>
Formally	O
,	O
the	O
natural	O
merge	B-Algorithm
sort	I-Algorithm
is	O
said	O
to	O
be	O
Runs-optimal	O
,	O
where	O
is	O
the	O
number	O
of	O
runs	B-Algorithm
in	O
,	O
minus	O
one	O
.	O
</s>
<s>
Tournament	B-Algorithm
replacement	I-Algorithm
selection	I-Algorithm
sorts	I-Algorithm
are	O
used	O
to	O
gather	O
the	O
initial	O
runs	B-Algorithm
for	O
external	B-Algorithm
sorting	I-Algorithm
algorithms	O
.	O
</s>
<s>
Instead	O
of	O
merging	O
two	O
blocks	O
at	O
a	O
time	O
,	O
a	O
ping-pong	O
merge	B-Algorithm
merges	O
four	O
blocks	O
at	O
a	O
time	O
.	O
</s>
<s>
An	O
early	O
public	O
domain	O
implementation	O
of	O
a	O
four-at-once	O
merge	B-Algorithm
was	O
by	O
WikiSort	O
in	O
2014	O
,	O
the	O
method	O
was	O
later	O
that	O
year	O
described	O
as	O
an	O
optimization	O
for	O
patience	O
sorting	B-Algorithm
and	O
named	O
a	O
ping-pong	O
merge	B-Algorithm
.	O
</s>
<s>
Quadsort	O
implemented	O
the	O
method	O
in	O
2020	O
and	O
named	O
it	O
a	O
quad	O
merge	B-Algorithm
.	O
</s>
<s>
One	O
drawback	O
of	O
merge	B-Algorithm
sort	I-Algorithm
,	O
when	O
implemented	O
on	O
arrays	O
,	O
is	O
its	O
working	O
memory	O
requirement	O
.	O
</s>
<s>
Several	O
methods	O
to	O
reduce	O
memory	O
or	O
make	O
merge	B-Algorithm
sort	I-Algorithm
fully	O
in-place	B-Algorithm
have	O
been	O
suggested	O
:	O
</s>
<s>
suggested	O
an	O
alternative	O
version	O
of	O
merge	B-Algorithm
sort	I-Algorithm
that	O
uses	O
constant	O
additional	O
space	O
.	O
</s>
<s>
present	O
an	O
algorithm	O
that	O
requires	O
a	O
constant	O
amount	O
of	O
working	O
memory	O
:	O
enough	O
storage	O
space	O
to	O
hold	O
one	O
element	O
of	O
the	O
input	O
array	B-Data_Structure
,	O
and	O
additional	O
space	O
to	O
hold	O
pointers	O
into	O
the	O
input	O
array	B-Data_Structure
.	O
</s>
<s>
Several	O
attempts	O
have	O
been	O
made	O
at	O
producing	O
an	O
in-place	B-Algorithm
merge	B-Algorithm
algorithm	I-Algorithm
that	O
can	O
be	O
combined	O
with	O
a	O
standard	O
(	O
top-down	O
or	O
bottom-up	O
)	O
merge	B-Algorithm
sort	I-Algorithm
to	O
produce	O
an	O
in-place	B-Algorithm
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
In	O
this	O
case	O
,	O
the	O
notion	O
of	O
"	O
in-place	B-Algorithm
"	O
can	O
be	O
relaxed	O
to	O
mean	O
"	O
taking	O
logarithmic	O
stack	B-Application
space	O
"	O
,	O
because	O
standard	O
merge	B-Algorithm
sort	I-Algorithm
requires	O
that	O
amount	O
of	O
space	O
for	O
its	O
own	O
stack	B-Application
usage	O
.	O
</s>
<s>
that	O
in-place	B-Algorithm
,	O
stable	O
merging	O
is	O
possible	O
in	O
time	O
using	O
a	O
constant	O
amount	O
of	O
scratch	O
space	O
,	O
but	O
their	O
algorithm	O
is	O
complicated	O
and	O
has	O
high	O
constant	O
factors	O
:	O
merging	O
arrays	O
of	O
length	O
and	O
can	O
take	O
moves	O
.	O
</s>
<s>
This	O
high	O
constant	O
factor	O
and	O
complicated	O
in-place	B-Algorithm
algorithm	I-Algorithm
was	O
made	O
simpler	O
and	O
easier	O
to	O
understand	O
.	O
</s>
<s>
Bing-Chao	O
Huang	O
and	O
Michael	O
A	O
.	O
Langston	O
presented	O
a	O
straightforward	O
linear	O
time	O
algorithm	O
practical	O
in-place	B-Algorithm
merge	B-Algorithm
to	O
merge	B-Algorithm
a	O
sorted	B-Algorithm
list	I-Algorithm
using	O
fixed	O
amount	O
of	O
additional	O
space	O
.	O
</s>
<s>
The	O
algorithm	O
takes	O
little	O
more	O
average	B-General_Concept
time	O
than	O
standard	O
merge	B-Algorithm
sort	I-Algorithm
algorithms	O
,	O
free	O
to	O
exploit	O
O(n )	O
temporary	O
extra	O
memory	O
cells	O
,	O
by	O
less	O
than	O
a	O
factor	O
of	O
two	O
.	O
</s>
<s>
Other	O
in-place	B-Algorithm
algorithms	I-Algorithm
include	O
SymMerge	O
,	O
which	O
takes	O
time	O
in	O
total	O
and	O
is	O
stable	O
.	O
</s>
<s>
Plugging	O
such	O
an	O
algorithm	O
into	O
merge	B-Algorithm
sort	I-Algorithm
increases	O
its	O
complexity	O
to	O
the	O
non-linearithmic	O
,	O
but	O
still	O
quasilinear	O
,	O
.	O
</s>
<s>
Many	O
applications	O
of	O
external	B-Algorithm
sorting	I-Algorithm
use	O
a	O
form	O
of	O
merge	B-Algorithm
sorting	B-Algorithm
where	O
the	O
input	O
get	O
split	O
up	O
to	O
a	O
higher	O
number	O
of	O
sublists	O
,	O
ideally	O
to	O
a	O
number	O
for	O
which	O
merging	O
them	O
still	O
makes	O
the	O
currently	O
processed	O
set	O
of	O
pages	B-General_Concept
fit	O
into	O
main	O
memory	O
.	O
</s>
<s>
A	O
modern	O
stable	O
linear	O
and	O
in-place	B-Algorithm
merge	B-Algorithm
variant	O
is	O
block	B-Algorithm
merge	I-Algorithm
sort	I-Algorithm
which	O
creates	O
a	O
section	O
of	O
unique	O
values	O
to	O
use	O
as	O
swap	O
space	O
.	O
</s>
<s>
This	O
field	O
will	O
be	O
used	O
to	O
link	O
the	O
keys	O
and	O
any	O
associated	O
information	O
together	O
in	O
a	O
sorted	B-Algorithm
list	I-Algorithm
(	O
a	O
key	O
and	O
its	O
related	O
information	O
is	O
called	O
a	O
record	O
)	O
.	O
</s>
<s>
Then	O
the	O
merging	O
of	O
the	O
sorted	B-Algorithm
lists	I-Algorithm
proceeds	O
by	O
changing	O
the	O
link	O
values	O
;	O
no	O
records	O
need	O
to	O
be	O
moved	O
at	O
all	O
.	O
</s>
<s>
This	O
is	O
a	O
standard	O
sorting	B-Algorithm
technique	O
,	O
not	O
restricted	O
to	O
merge	B-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
A	O
simple	O
way	O
to	O
reduce	O
the	O
space	O
overhead	O
to	O
n/2	O
is	O
to	O
maintain	O
left	O
and	O
right	O
as	O
a	O
combined	O
structure	O
,	O
copy	O
only	O
the	O
left	O
part	O
of	O
m	O
into	O
temporary	O
space	O
,	O
and	O
to	O
direct	O
the	O
merge	B-Algorithm
routine	O
to	O
place	O
the	O
merged	O
output	O
into	O
m	O
.	O
With	O
this	O
version	O
it	O
is	O
better	O
to	O
allocate	O
the	O
temporary	O
space	O
outside	O
the	O
merge	B-Algorithm
routine	O
,	O
so	O
that	O
only	O
one	O
allocation	O
is	O
needed	O
.	O
</s>
<s>
The	O
excessive	O
copying	O
mentioned	O
previously	O
is	O
also	O
mitigated	O
,	O
since	O
the	O
last	O
pair	O
of	O
lines	O
before	O
the	O
return	O
result	O
statement	O
(	O
function	O
merge	B-Algorithm
in	O
the	O
pseudo	B-Language
code	I-Language
above	O
)	O
become	O
superfluous	O
.	O
</s>
<s>
An	O
external	B-Algorithm
merge	I-Algorithm
sort	O
is	O
practical	O
to	O
run	O
using	O
disk	B-Device
or	O
tape	O
drives	O
when	O
the	O
data	O
to	O
be	O
sorted	O
is	O
too	O
large	O
to	O
fit	O
into	O
memory	O
.	O
</s>
<s>
External	B-Algorithm
sorting	I-Algorithm
explains	O
how	O
merge	B-Algorithm
sort	I-Algorithm
is	O
implemented	O
with	O
disk	B-Device
drives	I-Device
.	O
</s>
<s>
Merge	B-Algorithm
pairs	O
of	O
records	O
from	O
A	O
;	O
writing	O
two-record	O
sublists	O
alternately	O
to	O
C	O
and	O
D	O
.	O
</s>
<s>
Merge	B-Algorithm
two-record	O
sublists	O
from	O
C	O
and	O
D	O
into	O
four-record	O
sublists	O
;	O
writing	O
these	O
alternately	O
to	O
A	O
and	O
B	O
.	O
</s>
<s>
Instead	O
of	O
starting	O
with	O
very	O
short	O
runs	B-Algorithm
,	O
usually	O
a	O
hybrid	B-Algorithm
algorithm	I-Algorithm
is	O
used	O
,	O
where	O
the	O
initial	O
pass	O
will	O
read	O
many	O
records	O
into	O
memory	O
,	O
do	O
an	O
internal	O
sort	O
to	O
create	O
a	O
long	O
run	O
,	O
and	O
then	O
distribute	O
those	O
long	O
runs	B-Algorithm
onto	O
the	O
output	O
set	O
.	O
</s>
<s>
In	O
fact	O
,	O
there	O
are	O
techniques	O
that	O
can	O
make	O
the	O
initial	O
runs	B-Algorithm
longer	O
than	O
the	O
available	O
internal	O
memory	O
.	O
</s>
<s>
One	O
of	O
them	O
,	O
the	O
Knuth	O
's	O
'	O
snowplow	O
 '	O
(	O
based	O
on	O
a	O
binary	B-Application
min-heap	I-Application
)	O
,	O
generates	O
runs	B-Algorithm
twice	O
as	O
long	O
(	O
on	O
average	B-General_Concept
)	O
as	O
a	O
size	O
of	O
memory	O
used	O
.	O
</s>
<s>
O(n log n )	O
running	O
time	O
can	O
also	O
be	O
achieved	O
using	O
two	O
queues	B-Application
,	O
or	O
a	O
stack	B-Application
and	O
a	O
queue	B-Application
,	O
or	O
three	O
stacks	B-Application
.	O
</s>
<s>
In	O
the	O
other	O
direction	O
,	O
using	O
k	O
>	O
two	O
tapes	O
(	O
and	O
O(k )	O
items	O
in	O
memory	O
)	O
,	O
we	O
can	O
reduce	O
the	O
number	O
of	O
tape	O
operations	O
in	O
O(log k )	O
times	O
by	O
using	O
a	O
k/2	B-Algorithm
-way	I-Algorithm
merge	I-Algorithm
.	O
</s>
<s>
A	O
more	O
sophisticated	O
merge	B-Algorithm
sort	I-Algorithm
that	O
optimizes	O
tape	O
(	O
and	O
disk	B-Device
)	O
drive	O
usage	O
is	O
the	O
polyphase	B-Algorithm
merge	I-Algorithm
sort	I-Algorithm
.	O
</s>
<s>
On	O
modern	O
computers	O
,	O
locality	B-General_Concept
of	I-General_Concept
reference	I-General_Concept
can	O
be	O
of	O
paramount	O
importance	O
in	O
software	O
optimization	O
,	O
because	O
multilevel	O
memory	B-General_Concept
hierarchies	I-General_Concept
are	O
used	O
.	O
</s>
<s>
Cache-aware	O
versions	O
of	O
the	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
,	O
whose	O
operations	O
have	O
been	O
specifically	O
chosen	O
to	O
minimize	O
the	O
movement	O
of	O
pages	B-General_Concept
in	O
and	O
out	O
of	O
a	O
machine	O
's	O
memory	B-General_Concept
cache	I-General_Concept
,	O
have	O
been	O
proposed	O
.	O
</s>
<s>
For	O
example	O
,	O
the	O
algorithm	O
stops	O
partitioning	O
subarrays	O
when	O
subarrays	O
of	O
size	O
S	O
are	O
reached	O
,	O
where	O
S	O
is	O
the	O
number	O
of	O
data	O
items	O
fitting	O
into	O
a	O
CPU	O
's	O
cache	B-General_Concept
.	O
</s>
<s>
Each	O
of	O
these	O
subarrays	O
is	O
sorted	O
with	O
an	O
in-place	B-Algorithm
sorting	I-Algorithm
algorithm	I-Algorithm
such	O
as	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
to	O
discourage	O
memory	O
swaps	O
,	O
and	O
normal	O
merge	B-Algorithm
sort	I-Algorithm
is	O
then	O
completed	O
in	O
the	O
standard	O
recursive	O
fashion	O
.	O
</s>
<s>
This	O
algorithm	O
has	O
demonstrated	O
better	O
performance	O
on	O
machines	O
that	O
benefit	O
from	O
cache	B-General_Concept
optimization	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
parallelizes	O
well	O
due	O
to	O
the	O
use	O
of	O
the	O
divide-and-conquer	B-Algorithm
method	I-Algorithm
.	O
</s>
<s>
Some	O
parallel	O
merge	B-Algorithm
sort	I-Algorithm
algorithms	O
are	O
strongly	O
related	O
to	O
the	O
sequential	O
top-down	O
merge	B-Algorithm
algorithm	I-Algorithm
while	O
others	O
have	O
a	O
different	O
general	O
structure	O
and	O
use	O
the	O
K-way	B-Algorithm
merge	I-Algorithm
method	O
.	O
</s>
<s>
The	O
sequential	O
merge	B-Algorithm
sort	I-Algorithm
procedure	O
can	O
be	O
described	O
in	O
two	O
phases	O
,	O
the	O
divide	O
phase	O
and	O
the	O
merge	B-Algorithm
phase	O
.	O
</s>
<s>
Following	O
pseudocode	B-Language
describes	O
the	O
merge	B-Algorithm
sort	I-Algorithm
with	O
parallel	O
recursion	O
using	O
the	O
fork	B-Operating_System
and	I-Operating_System
join	I-Operating_System
keywords	O
:	O
</s>
<s>
//	O
Sort	O
elements	O
lo	O
through	O
hi	O
(	O
exclusive	O
)	O
of	O
array	B-Data_Structure
A	O
.	O
</s>
<s>
This	O
is	O
mainly	O
due	O
to	O
the	O
sequential	O
merge	B-Algorithm
method	O
,	O
as	O
it	O
is	O
the	O
bottleneck	O
of	O
the	O
parallel	O
executions	O
.	O
</s>
<s>
Better	O
parallelism	O
can	O
be	O
achieved	O
by	O
using	O
a	O
parallel	O
merge	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
For	O
the	O
partial	O
sequences	O
of	O
the	O
smaller	O
and	O
larger	O
elements	O
created	O
in	O
this	O
way	O
,	O
the	O
merge	B-Algorithm
algorithm	I-Algorithm
is	O
again	O
executed	O
in	O
parallel	O
until	O
the	O
base	O
case	O
of	O
the	O
recursion	O
is	O
reached	O
.	O
</s>
<s>
The	O
following	O
pseudocode	B-Language
shows	O
the	O
modified	O
parallel	O
merge	B-Algorithm
sort	I-Algorithm
method	O
using	O
the	O
parallel	O
merge	B-Algorithm
algorithm	I-Algorithm
(	O
adopted	O
from	O
Cormen	O
et	O
al	O
.	O
</s>
<s>
For	O
detailed	O
information	O
about	O
the	O
complexity	O
of	O
the	O
parallel	O
merge	B-Algorithm
procedure	O
,	O
see	O
Merge	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
This	O
parallel	O
merge	B-Algorithm
algorithm	I-Algorithm
reaches	O
a	O
parallelism	O
of	O
,	O
which	O
is	O
much	O
higher	O
than	O
the	O
parallelism	O
of	O
the	O
previous	O
algorithm	O
.	O
</s>
<s>
Such	O
a	O
sort	O
can	O
perform	O
well	O
in	O
practice	O
when	O
combined	O
with	O
a	O
fast	O
stable	O
sequential	O
sort	O
,	O
such	O
as	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
and	O
a	O
fast	O
sequential	O
merge	B-Algorithm
as	O
a	O
base	O
case	O
for	O
merging	O
small	O
arrays	O
.	O
</s>
<s>
It	O
seems	O
arbitrary	O
to	O
restrict	O
the	O
merge	B-Algorithm
sort	I-Algorithm
algorithms	O
to	O
a	O
binary	O
merge	B-Algorithm
method	O
,	O
since	O
there	O
are	O
usually	O
p	O
>	O
2	O
processors	O
available	O
.	O
</s>
<s>
A	O
better	O
approach	O
may	O
be	O
to	O
use	O
a	O
K-way	B-Algorithm
merge	I-Algorithm
method	O
,	O
a	O
generalization	O
of	O
binary	O
merge	B-Algorithm
,	O
in	O
which	O
sorted	O
sequences	O
are	O
merged	O
.	O
</s>
<s>
This	O
merge	B-Algorithm
variant	O
is	O
well	O
suited	O
to	O
describe	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
on	O
a	O
PRAM	B-Operating_System
.	O
</s>
<s>
These	O
elements	O
are	O
distributed	O
equally	O
among	O
all	O
processors	O
and	O
sorted	O
locally	O
using	O
a	O
sequential	O
Sorting	B-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
algorithm	O
is	O
perfectly	O
load-balanced	B-Application
.	O
</s>
<s>
Hence	O
,	O
each	O
processor	O
performs	O
the	O
p-way	O
merge	B-Algorithm
locally	O
and	O
thus	O
obtains	O
a	O
sorted	O
sequence	O
from	O
its	O
sub-sequences	O
.	O
</s>
<s>
Because	O
of	O
the	O
second	O
property	O
,	O
no	O
further	O
p-way-merge	O
has	O
to	O
be	O
performed	O
,	O
the	O
results	O
only	O
have	O
to	O
be	O
put	O
together	O
in	O
the	O
order	O
of	O
the	O
processor	O
number	O
.	O
</s>
<s>
For	O
the	O
complexity	O
analysis	O
the	O
PRAM	B-Operating_System
model	O
is	O
chosen	O
.	O
</s>
<s>
The	O
expected	O
recursion	O
depth	O
is	O
as	O
in	O
the	O
ordinary	O
Quickselect	B-Algorithm
.	O
</s>
<s>
Applied	O
on	O
the	O
parallel	O
multiway	O
merge	B-Algorithm
sort	I-Algorithm
,	O
this	O
algorithm	O
has	O
to	O
be	O
invoked	O
in	O
parallel	O
such	O
that	O
all	O
splitter	O
elements	O
of	O
rank	O
for	O
are	O
found	O
simultaneously	O
.	O
</s>
<s>
Below	O
,	O
the	O
complete	O
pseudocode	B-Language
of	O
the	O
parallel	O
multiway	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
is	O
given	O
.	O
</s>
<s>
Firstly	O
,	O
each	O
processor	O
sorts	O
the	O
assigned	O
elements	O
locally	O
using	O
a	O
sorting	B-Algorithm
algorithm	I-Algorithm
with	O
complexity	O
.	O
</s>
<s>
Finally	O
,	O
each	O
group	O
of	O
splits	O
have	O
to	O
be	O
merged	O
in	O
parallel	O
by	O
each	O
processor	O
with	O
a	O
running	O
time	O
of	O
using	O
a	O
sequential	O
p-way	B-Algorithm
merge	I-Algorithm
algorithm	I-Algorithm
.	O
</s>
<s>
The	O
multiway	O
merge	B-Algorithm
sort	I-Algorithm
algorithm	O
is	O
very	O
scalable	O
through	O
its	O
high	O
parallelization	O
capability	O
,	O
which	O
allows	O
the	O
use	O
of	O
many	O
processors	O
.	O
</s>
<s>
This	O
makes	O
the	O
algorithm	O
a	O
viable	O
candidate	O
for	O
sorting	B-Algorithm
large	O
amounts	O
of	O
data	O
,	O
such	O
as	O
those	O
processed	O
in	O
computer	B-Architecture
clusters	I-Architecture
.	O
</s>
<s>
Also	O
,	O
since	O
in	O
such	O
systems	O
memory	O
is	O
usually	O
not	O
a	O
limiting	O
resource	O
,	O
the	O
disadvantage	O
of	O
space	O
complexity	O
of	O
merge	B-Algorithm
sort	I-Algorithm
is	O
negligible	O
.	O
</s>
<s>
However	O
,	O
other	O
factors	O
become	O
important	O
in	O
such	O
systems	O
,	O
which	O
are	O
not	O
taken	O
into	O
account	O
when	O
modelling	O
on	O
a	O
PRAM	B-Operating_System
.	O
</s>
<s>
Here	O
,	O
the	O
following	O
aspects	O
need	O
to	O
be	O
considered	O
:	O
Memory	B-General_Concept
hierarchy	I-General_Concept
,	O
when	O
the	O
data	O
does	O
not	O
fit	O
into	O
the	O
processors	O
cache	B-General_Concept
,	O
or	O
the	O
communication	O
overhead	O
of	O
exchanging	O
data	O
between	O
processors	O
,	O
which	O
could	O
become	O
a	O
bottleneck	O
when	O
the	O
data	O
can	O
no	O
longer	O
be	O
accessed	O
via	O
the	O
shared	O
memory	O
.	O
</s>
<s>
have	O
presented	O
in	O
their	O
paper	O
a	O
bulk	B-Application
synchronous	I-Application
parallel	I-Application
algorithm	O
for	O
multilevel	O
multiway	O
mergesort	B-Algorithm
,	O
which	O
divides	O
processors	O
into	O
groups	O
of	O
size	O
.	O
</s>
<s>
Unlike	O
single	O
level	O
multiway	O
mergesort	B-Algorithm
,	O
these	O
sequences	O
are	O
then	O
partitioned	O
into	O
parts	O
and	O
assigned	O
to	O
the	O
appropriate	O
processor	O
groups	O
.	O
</s>
<s>
racks	B-Application
,	O
clusters	B-Architecture
,...	O
)	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
was	O
one	O
of	O
the	O
first	O
sorting	B-Algorithm
algorithms	I-Algorithm
where	O
optimal	O
speed	O
up	O
was	O
achieved	O
,	O
with	O
Richard	O
Cole	O
using	O
a	O
clever	O
subsampling	O
algorithm	O
to	O
ensure	O
O(1 )	O
merge	B-Algorithm
.	O
</s>
<s>
Other	O
sophisticated	O
parallel	O
sorting	B-Algorithm
algorithms	I-Algorithm
can	O
achieve	O
the	O
same	O
or	O
better	O
time	O
bounds	O
with	O
a	O
lower	O
constant	O
.	O
</s>
<s>
For	O
example	O
,	O
in	O
1991	O
David	O
Powers	O
described	O
a	O
parallelized	O
quicksort	B-Algorithm
(	O
and	O
a	O
related	O
radix	B-Algorithm
sort	I-Algorithm
)	O
that	O
can	O
operate	O
in	O
O(log n )	O
time	O
on	O
a	O
CRCW	B-Operating_System
parallel	B-Operating_System
random-access	I-Operating_System
machine	I-Operating_System
(	O
PRAM	B-Operating_System
)	O
with	O
n	O
processors	O
by	O
performing	O
partitioning	O
implicitly	O
.	O
</s>
<s>
Powers	O
further	O
shows	O
that	O
a	O
pipelined	O
version	O
of	O
Batcher	O
's	O
Bitonic	B-Algorithm
Mergesort	I-Algorithm
at	O
O((log n )	O
2	O
)	O
time	O
on	O
a	O
butterfly	O
sorting	B-Algorithm
network	I-Algorithm
is	O
in	O
practice	O
actually	O
faster	O
than	O
his	O
O(log n )	O
sorts	O
on	O
a	O
PRAM	B-Operating_System
,	O
and	O
he	O
provides	O
detailed	O
discussion	O
of	O
the	O
hidden	O
overheads	O
in	O
comparison	O
,	O
radix	O
and	O
parallel	O
sorting	B-Algorithm
.	O
</s>
<s>
Although	O
heapsort	B-Application
has	O
the	O
same	O
time	O
bounds	O
as	O
merge	B-Algorithm
sort	I-Algorithm
,	O
it	O
requires	O
only	O
Θ(1 )	O
auxiliary	O
space	O
instead	O
of	O
merge	B-Algorithm
sort	I-Algorithm
's	O
Θ(n )	O
.	O
</s>
<s>
On	O
typical	O
modern	O
architectures	O
,	O
efficient	O
quicksort	B-Algorithm
implementations	O
generally	O
outperform	O
merge	B-Algorithm
sort	I-Algorithm
for	O
sorting	B-Algorithm
RAM-based	O
arrays	O
.	O
</s>
<s>
On	O
the	O
other	O
hand	O
,	O
merge	B-Algorithm
sort	I-Algorithm
is	O
a	O
stable	O
sort	O
and	O
is	O
more	O
efficient	O
at	O
handling	O
slow-to-access	O
sequential	O
media	O
.	O
</s>
<s>
Merge	B-Algorithm
sort	I-Algorithm
is	O
often	O
the	O
best	O
choice	O
for	O
sorting	B-Algorithm
a	O
linked	B-Data_Structure
list	I-Data_Structure
:	O
in	O
this	O
situation	O
it	O
is	O
relatively	O
easy	O
to	O
implement	O
a	O
merge	B-Algorithm
sort	I-Algorithm
in	O
such	O
a	O
way	O
that	O
it	O
requires	O
only	O
Θ(1 )	O
extra	O
space	O
,	O
and	O
the	O
slow	O
random-access	O
performance	O
of	O
a	O
linked	B-Data_Structure
list	I-Data_Structure
makes	O
some	O
other	O
algorithms	O
(	O
such	O
as	O
quicksort	B-Algorithm
)	O
perform	O
poorly	O
,	O
and	O
others	O
(	O
such	O
as	O
heapsort	B-Application
)	O
completely	O
impossible	O
.	O
</s>
<s>
As	O
of	O
Perl	B-Language
5.8	O
,	O
merge	B-Algorithm
sort	I-Algorithm
is	O
its	O
default	O
sorting	B-Algorithm
algorithm	I-Algorithm
(	O
it	O
was	O
quicksort	B-Algorithm
in	O
previous	O
versions	O
of	O
Perl	B-Language
)	O
.	O
</s>
<s>
In	O
Java	B-Device
,	O
the	O
methods	O
use	O
merge	B-Algorithm
sort	I-Algorithm
or	O
a	O
tuned	O
quicksort	B-Algorithm
depending	O
on	O
the	O
datatypes	O
and	O
for	O
implementation	O
efficiency	O
switch	O
to	O
insertion	B-Algorithm
sort	I-Algorithm
when	O
fewer	O
than	O
seven	O
array	B-Data_Structure
elements	I-Data_Structure
are	O
being	O
sorted	O
.	O
</s>
<s>
The	O
Linux	B-Application
kernel	O
uses	O
merge	B-Algorithm
sort	I-Algorithm
for	O
its	O
linked	B-Data_Structure
lists	I-Data_Structure
.	O
</s>
<s>
Python	B-Language
uses	O
Timsort	B-Algorithm
,	O
another	O
tuned	O
hybrid	O
of	O
merge	B-Algorithm
sort	I-Algorithm
and	O
insertion	B-Algorithm
sort	I-Algorithm
,	O
that	O
has	O
become	O
the	O
standard	O
sort	B-Algorithm
algorithm	I-Algorithm
in	O
Java	B-Device
SE	O
7	O
(	O
for	O
arrays	O
of	O
non-primitive	O
types	O
)	O
,	O
on	O
the	O
Android	B-Application
platform	I-Application
,	O
and	O
in	O
GNU	B-Language
Octave	I-Language
.	O
</s>
